- 只有UITextView可以做到。
- 要實作UITextViewDelegate
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange {
//可以做些判斷
return YES;
}
- 加入某段文字
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
[attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://yourUrl.com"] range:[str rangeOfString:@"Knowledge base"]];
self.linkTextView.attributedText = attrStr;
- 整段文字
NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:@"please visit our Knowledge base" attributes:dictAttr];
self.linkTextView.attributedText = attrStr;
沒有留言:
張貼留言