本文介绍了如何更改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 协议参考:

最后一个参数是一个名为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超链接选择背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:00