- (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;
}
关键代码
NSString *htmlString = [NSString stringWithFormat:@"在信息科技时代,秉持着“以创新科技立业,为客户创造价值,塑造品牌传播力”的理念,对外输出先进的移动端信息技术;如有疑问您可以访问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;
}