想要使用swift在html字符串中转换h3标签的颜色

时间:2016-05-23 06:07:54

标签: swift ios7 xcode7

我有一个html字符串,我必须在文本视图中显示我已经在NSMutableString中转换它但在该字符串中有多个标题即将到来。我想用蓝色改变标题的颜色。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您可以像这样在h3标签中应用颜色

let htmlString: String = "<html><body> <h3><font color=black>Some html string</font></h3> </body></html>"

let temphtmlString : NSString = (htmlString as NSString).stringByReplacingOccurrencesOfString("color=black", withString: "color=blue")


  let attrStr = try! NSAttributedString(
                data: temphtmlString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
                options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                documentAttributes: nil)
txtView.attributedText = attrStr