iOS UITextView 設置 NSLinkAttributeName 屬性,點擊鏈接跳轉


 

@interface ViewController ()<UITextViewDelegate>


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"www.google.com"];
    NSDictionary *linkDic = @{ NSLinkAttributeName : [NSURL URLWithString:@"http://www.google.com"] };
    [str setAttributes:linkDic range:[[str string] rangeOfString:@"www.google.com"]];
    _textView.attributedText = str;
}


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    NSLog(@"=============%@",URL);
    return YES;
}


需要注意的是 將上述代碼 需做如下調整 我們需要的是 點擊跳轉 而不是點擊編輯 所以需要關閉編輯屬性
如下:

在 IB 中設置以下 > 實用程序 > 屬性檢查器。值得注意的是,UITextView不能是可編輯,啟用鏈接。

UITextView Settings

你也可以做同樣的代碼:

_textView.editable = NO;
_textView.dataDetectorTypes = UIDataDetectorTypeLink;
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM