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

查看:328
本文介绍了如何更改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.您可以使用此方法来触发 替代操作,例如将网页内容显示在网址中的 当前应用程序中的网络视图.

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天全站免登陆