在swift 3中的textview中显示属性文本?

时间:2017-06-06 10:47:23

标签: html swift3 textview

我想以Italic显示文本,从服务器接收粗体。 < p> < b> <我>你好< / i> < / b> <我>世界< / i>< / p>所以,

responseObj!["text"] = "<p><b><i>hello</i></b><i>world</></p>"
     if let postText:String = responseObj!["text"] as? String{
          let str = try NSAttributedString(data: postText.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
      self.textView.attributedText = str
    }

当我添加这样的意思是我的文字显示正常而不应用粗体&amp;斜体。我希望文本以粗体,斜体显示。

2 个答案:

答案 0 :(得分:3)

编辑:更新了Swift 4

extension

创建String
extension String {
    func htmlAttributedString(fontSize: CGFloat = 17.0) -> NSAttributedString? {
        let fontName = UIFont.systemFont(ofSize: fontSize).fontName
        let string = self.appending(String(format: "<style>body{font-family: '%@'; font-size:%fpx;}</style>", fontName, fontSize))
        guard let data = string.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }

        guard let html = try? NSMutableAttributedString (
             data: data,
             options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
             documentAttributes: nil) else { return nil }
        return html
    }
}

在项目的任何位置调用它:

text.htmlAttributedString() // will set fontSize with 17.0 which is the default value


text.htmlAttributedString(fontSize: 14.0) // pass your required fontSize
  

请注意,let string = self.appending(String(format: "<style>body{font-family: '%@'; font-size:%fpx;}</style>", fontName, fontSize))用于保持字符串字体与默认iOS平台中使用的字体相同。

要在UITextView上设置字符串,请使用:

textView.attributedText = textToBeConverted.htmlAttributedString() ?? ""

<强>结果:

RESULT

答案 1 :(得分:0)

将归属文本从下面的函数

分配给UITextView
//HTML to Attributed text
func stringFromHtml(string: String) -> NSAttributedString? {
do {
    let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
    if let d = data {
        let str = try NSAttributedString(data: d,
                                         options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                         documentAttributes: nil)
        return str
    }
} catch {
}
return nil
}

//This will give you attributed text which you have to assign to your UITextView. and whatever string having html tag that you have to pass inside this function.

//Below code will conver attributed text to HTML Text
//Attributed to HTML text

    let attrStr = self.txtView.attributedText
    let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
    do {
        let htmlData = try attrStr?.data(from: NSMakeRange(0, (attrStr?.length)!), documentAttributes:documentAttributes)
        if let htmlString = String(data:htmlData!, encoding:String.Encoding.utf8) {
            print(htmlString)
        }
    }
    catch {
        print("error creating HTML from Attributed String")
    }