在UITextView中更改一个链接的属性
我有一个具有多个URL的UITextView
,通过将dataDetectorTypes
属性设置为dataDetectorTypes
来UIDataDetectorTypeLink
。 然后我使用linkTextAttributes
属性来设置链接的颜色。 现在,当用户点击其中一个链接(使用UITapGestureRecognizer
),我想只改变该链接的颜色。 如果我改变linkTextAttributes
,所有的链接将改变颜色。
我怎样才能改变被点击的链接的颜色?
如果这些url是固定的。 例如:我有以下url:
我会把它们放到一个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;
干杯!
我想我解决了它,使用UITextView
一个子类,它有一个rangeOfLink
属性。
首先,在我的UIViewController
viewDidLoad:
,我添加
self.textView.dataDetectorTypes = UIDataDetectorTypeLink; // change for other link types self.textView.selectable = YES; self.textView.userInteractionEnabled = YES; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap:)]; tapGesture.cancelsTouchesInView = YES; [self.textView addGestureRecognizer: tapGesture]; [self.textView setNeedsDisplay]; // force a redraw so that drawRect is called
然后在handleTap
,我这样做:
MyTextViewWithLink *aTextView = (IDTextViewWithLink *) recognizer.view; if (aTextView != self.textView) return; if (recognizer.state == UIGestureRecognizerStateEnded) { CGPoint location = [recognizer locationInView: aTextView]; // this returns an NSTextCheckingResult if location is inside a link NSTextCheckingResult *result = [self textCheckingResultAtPoint: location inTextView: aTextView]; if (result) { aTextView.rangeOfLink = result.range; [aTextView setNeedsDisplay]; // this will force the color change // open url } }
最后我重写我的UITextView
子类中的drawRect
:
self.linkTextAttributes = [NSDictionary dictionary]; NSError *error = nil; NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes: NSTextCheckingTypeLink error: &error]; // change for other link types if (!error && dataDetector) { NSArray* resultString = [dataDetector matchesInString: self.text options: NSMatchingReportProgress range: NSMakeRange(0, [self.text length])]; if (resultString.count > 0) { NSMutableAttributedString *mas = [self.attributedText mutableCopy]; for (NSTextCheckingResult* result in resultString) { if (result.resultType == NSTextCheckingTypeLink) { NSRange intersection = NSIntersectionRange(result.range, self.rangeOfLink); if (intersection.length <= 0) // no match [mas addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: self.rangeOfLink]; else [mas addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: self.rangeOfLink]; } } self.attributedText = mas; } } [super drawRect: rect];
现在,如果textView有多个链接,只有选定的一个会改变颜色。