2015-04-06 45 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