2015-04-06 46 views
1

我有一个UITextView多个URL,我可以通过将dataDetectorTypes属性设置为UIDataDetectorTypeLink来激活它。然后我使用linkTextAttributes属性来设置链接的颜色。现在,当用户点击其中一个链接(使用UITapGestureRecognizer)时,我只想更改该链接的颜色。如果我更改linkTextAttributes,所有链接都会改变颜色。更改UITextView中的一个链接的属性

如何更改所点击链接的颜色?

回答

0

我想我解决了它,使用UITextView的子类调用,它具有rangeOfLink属性。

首先,我UIViewControllerviewDidLoad:,我在handleTap添加

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 

然后,我这样做:

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]; 

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

现在如果textView有多于一个链接,只有选定的链接会改变颜色。

0

如果这些网址是固定的。 例如: 我有以下网址:

我会把他们的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; 

干杯!

+0

不知道这是如何让我改变所选网址的颜色。此外,这些url已经在我的CoreData模型的一部分'NSString'中,所以我不认为我可以使用'appendAttributedString:'等来构造'NSMutableAttributedString'。 – Koen 2015-04-06 16:24:27

+0

基本上没有改变UITextView访问链接的链接属性。 ([这里检查](https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/doc/constant_group/Character_Attributes)) 但有一个委托UITextView捕获被点击的URL。这个想法是让这些字符串重新分配给UITextView。 否则,只需使用UIWebView,然后格式化HTML字符串,我认为这是一个更难的解决方案。 – 2015-04-06 16:32:06

+0

是的,我之前看过'shouldInteractWithURL:',但效果不好,我转向了手势识别器。但我会重新审视它,因为它听起来像是改变颜色的好地方。 – Koen 2015-04-06 17:02:36