且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测 UITextView 上的选择更改?

更新时间:2023-02-26 14:18:44

UITextView 采用 UITextInput 协议.该协议包括必需的 selectedTextRange 读/写属性.这就是您要响应更改的属性.

UITextView adopts the UITextInput protocol. That protocol includes a required selectedTextRange read/write property. That is the property you want to respond to changes of.

虽然您可以观察到该属性,但最简单的方法是覆盖子类中的 -setSelectedTextRange: 方法,并在其中执行您需要的任何其他行为.(根据您的特定设计,您可能想也可能不想确认范围确实在改变.)

While you could observe that property, the simplest approach is to override the -setSelectedTextRange: method in your subclass and perform whatever additional behaviours you need in there. (You may or may not want to confirm that the range is actually changing, depending on your particular design.)