我有一个UITextView
具有多个 URL,我通过设置激活dataDetectorTypes
财产给UIDataDetectorTypeLink
。然后我使用linkTextAttributes
属性来设置链接的颜色。现在,当用户点击其中一个链接时(使用UITapGestureRecognizer
),我只想更改该链接的颜色。如果我改变linkTextAttributes
,所有链接都会改变颜色。
如何仅更改点击链接的颜色?
如果这些网址是固定的。
例如:
我有以下网址:
-
http://www.123.com http://www.123.com
-
http://www.456.com http://www.456.com
- http://www.789.com http://www.789.com
我会将它们放入 NSAttributedString
使用 NSMutableAttributedString 将它们全部组合起来
NSMutableAttributedString *urlsAttributedText = [[NSMutableAttributedString alloc]init];
NSAttributedString *url1 = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"http://www.123.com\n", nil) attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:15.0f]}];
NSAttributedString *url2 = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"http://www.456.com\n", nil) attributes:@{NSForegroundColorAttributeName : [UIColor greenColor], NSFontAttributeName : [UIFont systemFontOfSize:15.0f]}];
NSAttributedString *url3 = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"http://www.789.com\n", nil) attributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:15.0f]}];
[urlsAttributedText url1];
[urlsAttributedText appendAttributedString:url2];
[urlsAttributedText appendAttributedString:url3];
self.texView.attributedText = urlsAttributedText;
Cheers!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)