美文网首页iOS 技术分享
iOS - UITextView 显示html

iOS - UITextView 显示html

作者: Joh蜗牛 | 来源:发表于2020-04-07 17:12 被阅读0次

    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;
    }
    

    相关文章

      网友评论

        本文标题:iOS - UITextView 显示html

        本文链接:https://www.haomeiwen.com/subject/vabnphtx.html