检测 UITextView 上的选择更改? [英] Detecting selection change on a UITextView?

查看:55
本文介绍了检测 UITextView 上的选择更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测用户何时更改了文本视图中所选文本的范围(在 UITextView 的子类中)?我找不到任何 NSNotification.我尝试观察键值,但未调用所选范围的观察者

Is there a way to detect when the user changes the range of the selected text in a textview (In a subclass of UITextView)? I couldn't find any NSNotification. I tried observing keyvalues but the observer for selected range does not get called

[self addObserver:self forKeyPath:@"selectedRange" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqual:@"selectedRange"])
    {
        // Do stuff
    }
}

我不想使用 UITextView 的委托方法

推荐答案

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.)

这篇关于检测 UITextView 上的选择更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆