UITextView具有可点击链接但没有文字突出显示 [英] UITextView with clickable links but no text highlighting

查看:106
本文介绍了UITextView具有可点击链接但没有文字突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示不可编辑文本的UITextView。我希望文本自动解析用户的链接,电话号码等,以及可点击的文本。

I have a UITextView displaying non-editable text. I want the text to automatically parse links, phone numbers, etc for the user, and for those to be clickable.

我不希望用户能够但是,突出显示文本,因为我想要覆盖那些长按和双击交互以执行不同的操作。

I don't want the user to be able to highlight text, though, because I want to override those long press and double-tap interactions to do something different.

为了在iOS7中解析链接,可选择需要为UITextView打开开关,但是Selectable也能启用突出显示,这是我不想要的。

In order for links to be parsed in iOS7, the Selectable switch needs to be turned on for the UITextView, but Selectable also enables highlighting, which I don't want.

我试图覆盖LongPress手势以防止突出显示,但是似乎也禁用了链接上的普通点击...

I tried overriding the LongPress gesture to prevent highlighting, but that seems to have disabled ordinary taps on links as well...

for (UIGestureRecognizer *recognizer in cell.messageTextView.gestureRecognizers) {
    if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
        recognizer.enabled = NO;
    }
    if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]){
        recognizer.enabled = YES;
    }
}

有很多类似的线程,但似乎没有解决这个特定的链接启用问题,文字不能突出显示。

There are lots of similar threads out there but none seem to address this specific question of links enabled, text not highlightable.

推荐答案

我正在研究完全相同的问题和最好的我可以做的是通过将以下内容添加到UITextView的委托来立即清除选择:

I am working on the exact same problem and the best I could do was to instantly clear the selection as soon as it is made by adding the following to the UITextView's delegate:

- (void)textViewDidChangeSelection:(UITextView *)textView {
    if(!NSEqualRanges(textView.selectedRange, NSMakeRange(0, 0))) {
        textView.selectedRange = NSMakeRange(0, 0);
    }
}

请注意检查以防止递归。这几乎解决了这个问题,因为只有选择被禁用 - 链接仍然有效。

Note the check to prevent recursion. This pretty much addresses the issue because only selection is disabled -- links will still work.

另一个切线问题是文本视图仍然会成为第一响应者,你可以通过在设置所选范围后设置所需的第一响应者来修复。

Another tangential issue is that the text view will still become first responder, which you can fix by setting your desired first responder after setting the selected range.

注意:剩下的唯一视觉怪异是按下并按住放大镜。

Note: the only visual oddity that remains is that press-and-hold brings up the magnifying glass.

这篇关于UITextView具有可点击链接但没有文字突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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