【问题标题】:Want to convert Colour of h3 tag heading in html string using swift想使用swift转换html字符串中h3标签标题的颜色
【发布时间】:2016-05-23 06:07:54
【问题描述】:

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

【问题讨论】:

    标签: swift ios7 xcode7


    【解决方案1】:

    你可以像这样在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
    

    【讨论】:

    • html 字符串来自服务器,标题标签颜色为黑色。
    • 您可以使用 stringByReplacingOccurrencesOfString 替换 html 字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多