如何更改 UITextView 超链接选择背景颜色? [英] How to change UITextView Hyperlink selection background color?

查看:57
本文介绍了如何更改 UITextView 超链接选择背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITextView 使主题标签可选.它可以完美地检测水龙头.我的问题是选择颜色.它看起来是黑色的,我希望文本在选择时淡化一点.这是我现在所拥有的:

I am using a UITextView to make hashtags selectable. It detects taps perfectly. My issue is with the selection color. It looks black and I wanted the text to fade a bit on selection instead. Here's what I've got now:

我已尝试更改 tintColorNSForegroundColorAttributeNameNSBackgroundColorAttributeName,但它不起作用.

I've tried changing the tintColor, the NSForegroundColorAttributeName, and the NSBackgroundColorAttributeName but it doesn't work.

推荐答案

对于检测到的链接的选定颜色或突出显示颜色没有记录的属性,但是您应该能够通过覆盖委托方法 来实现相同的效果textView:shouldInteractWithURL:inRange: 并自己更改颜色.

There is no documented property for the selected or highlighted color of detected links, but you should be able to achieve the same effect by overriding the delegate method textView:shouldInteractWithURL:inRange: and changing the color yourself.

来自 UITextViewDelegate 协议参考:

如果用户点击或长按文本视图会调用此方法网址链接.此方法的实现是可选的.默认情况下,文本视图打开负责处理 URL 类型的应用程序并将 URL 传递给它.您可以使用此方法来触发替代操作,例如在 URL 中显示 Web 内容当前应用程序中的网络视图.

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 之类的属性,以便仅更改被点击的特定主题标签的颜色.

The last parameter is an NSRange object called characterRange, which represents the character range containing the tapped URL (or hashtag). Using that range, you should be able to add attributes such as NSForegroundColorAttributeName so as to change only the color of the particular hashtag that was tapped.

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

You'll probably want to revert any changes on touchesEnded and touchesCancelled.

或者,您可以创建自己的子类并手动处理上述内容.

这篇关于如何更改 UITextView 超链接选择背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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