- (UITextView *)textView{
if (!_textView) {
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
_textView.backgroundColor = [UIColor whiteColor];
_textView.editable = false;
_textView.scrollEnabled = false;
_textView.delegate = self;
}
return _textView;
}
關(guān)鍵代碼
NSString *htmlString = [NSString stringWithFormat:@"在信息科技時(shí)代荞彼,秉持著“以創(chuàng)新科技立業(yè)菜谣,為客戶創(chuàng)造價(jià)值,塑造品牌傳播力”的理念,對外輸出先進(jìn)的移動(dòng)端信息技術(shù)导匣;如有疑問您可以訪問https://www.baidu.com"];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:htmlString];
[attributedString addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@""} range:NSMakeRange(5, 4)];
[attributedString addAttributes:@{NSLinkAttributeName:@""} range:NSMakeRange(27, 4)];
[attributedString addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSLinkAttributeName:@"https://www.baidu.com"} range:NSMakeRange(62, 21)];
NSMutableParagraphStyle *style = [attributedString attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:nil];
style.alignment = NSTextAlignmentLeft;
self.textView.attributedText = attributedString;
self.textView.font = [UIFont systemFontOfSize:17];
代理處理事件
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange: (NSRange)characterRange interaction:(UITextItemInteraction)interaction{
NSLog(@"url:%@, %@", URL, NSStringFromRange(characterRange));
return true;
}