ios - 如何更改 UITextView 超链接选择背景颜色?

标签 ios hyperlink uitextview uicolor

我正在使用 UITextView 来使主题标签可选。它可以完美地检测水龙头。我的问题是选择颜色。它看起来是黑色的,我希望文本在选择时稍微褪色。 这是我现在得到的:

screenshot

我尝试更改 tintColorNSForegroundColorAttributeNameNSBackgroundColorAttributeName,但不起作用。

最佳答案

检测到的链接的选定或突出显示颜色没有记录属性,但您应该能够通过重写委托(delegate)方法 textView:shouldInteractWithURL:inRange: 并更改颜色来实现相同的效果你自己。

来自UITextViewDelegate协议(protocol)引用:

The text view calls this method if the user taps or long-presses the URL link. Implementation of this method is optional. By default, the text view opens the application responsible for handling the URL type and passes it the URL. You can use this method to trigger an alternative action, such as displaying the web content at the URL in a web view within the current application.

最后一个参数是一个名为 characterRange 的 NSRange 对象,它表示包含点击的 URL(或主题标签)的字符范围。使用该范围,您应该能够添加诸如 NSForegroundColorAttributeName 之类的属性,以便仅更改所点击的特定主题标签的颜色。

您可能希望恢复对 touchesEndedtouchesCancelled 的任何更改。

或者,您可以 make your own subclass and manually handle the above .

关于ios - 如何更改 UITextView 超链接选择背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36596010/

相关文章:

ios - UITextView 内容在增加高度的同时改变位置

ios - UIAlertView 不打开 url?

Android - 超链接不可点击

ios - 根据标题 UITextview 内容更新 UICollectionView 标题高度

c# - 如何获取DataNavigateUrlFields 的值?

html - 如何创建带有下划线的链接,其中下划线被字母切割?

ios - 在编辑 UITextView 和触摸 TableView Controller 中键盘以外的任何地方时无法关闭键盘

ios - 第二次触摸被忽略

ios - 如何在 KeychainItemWrapper 中保存 bool 值

ios - Swift 可选值和 if let 语句