iOS 技术分享

iOS - UITextView 显示html

2020-04-07  本文已影响0人  Joh蜗牛

1.展示包含html的文字:

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
 self.contentTextView.attributedText = attrStr;

2.计算包含html的文字的高度:

CGFloat h = [attrStr boundingRectWithSize:CGSizeMake(ScreenWidth-75-20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height + 10;

3.获取文字中链接的点击事件:

self.contentTextView.delegate = self;

代理方式:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    NSLog(@"点击链接-%@",URL);
    
    return YES;
}
上一篇 下一篇

猜你喜欢

热点阅读