2015-08-21 59 views
0

我有一個UITextView,其中包含一個包含URL的屬性字符串。該屬性串被轉換成一個鏈接,但是當你按下鏈接,你得到一個錯誤:UITextView點擊時未打開URL

Unknown DDResult category 1 for result ; could not find any actions for URL www.myurl.com

這裏是我的代碼:

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"My Text", nil)]; 
NSRange range = [str.mutableString rangeOfString:@"www.myurl.com" options:NSCaseInsensitiveSearch]; 

if (range.location != NSNotFound) { 
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"CourierPrime-Bold" size:15] range:NSMakeRange(0, range.location)]; 
    [str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"www.myurl.com"] range:range]; 
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:range]; 
} 


self.welcomeText.editable = NO; 
self.welcomeText.attributedText = str; 

//As a note, I have tried this with the value 0 and UIDataDetectorTypeLink 
//and neither option worked. 
self.welcomeText.dataDetectorTypes = UIDataDetectorTypeAll; 

我使用的是XIB和行爲的選項我已啓用selectable,並在檢測選項中選擇了links

我希望鏈接可以在手機瀏覽器中打開,如果可能的話,而不是創建一個web視圖。

回答

4

URL需要一個方案。

此:

[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"www.myurl.com"] range:range]; 

需求是:

[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.myurl.com"] range:range]; 
+0

太謝謝你了!完美的作品。 – BlackHatSamurai