ios 显示html标签,超链接颜色以及下划线的处理

html-css07

ios 显示html标签,超链接颜色以及下划线的处理,第1张

有的时候,我们的app需要显示html标签的效果,这个时候,如果需求不是特别复杂的话,我们都是可以使用textView来实现的。对冉Label也可以实现,但是label很难实现超链接的点击效果,所以,还是建议使用textView。

normalFontheightWithWidth方法如下:(供参考)

附超链接的属性:

加油吧!

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 30, 20, 114)]

textView.backgroundColor = [UIColor redColor]

[self.view addSubview:textView]

NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>![]( https://img-blog.csdnimg.cn/20190927151043371.png )"

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil]

textView.attributedText = attributedString