【问题标题】:Display attributed text in textview in swift 3?在 swift 3 中的 textview 中显示属性文本?
【发布时间】:2017-11-07 08:21:37
【问题描述】:

我想以斜体显示文本,从服务器接收到的粗体。

你好世界

所以,
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
    }

当我像这样添加时,意味着我的文本显示正常而不应用粗体和斜体。我希望文本以粗体、斜体显示。

【问题讨论】:

    标签: html swift3 textview


    【解决方案1】:

    编辑:更新 Swift 4

    String 创建一个extension

    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: "&lt;style&gt;body{font-family: '%@'; font-size:%fpx;}&lt;/style&gt;", fontName, fontSize)) 用于保持字符串字体与默认 iOS 平台中使用的字体相同。

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

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

    结果:

    【讨论】:

    • 应该是字符串
    • ?? "" 用于解开变量中的值,其中"" 是默认值。在简单的英语中,如果textToBeConverted.htmlAttributedString() 为 nil,那么textView.attributedText 应该默认为""
    • 这种格式不适用

      体验版税i
      一个拥有自豪感的地址

      我尝试过使用 uiwebview loadHtmString 但无法正常工作,使用 uilabel 但没有运气。这个html格式有问题吗,这个格式怎么显示?
    • 您是否尝试过设置textViewattributeText 属性。
    【解决方案2】:

    从下面的函数将属性文本分配给 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")
        }
    

    【讨论】:

    • 我只需要你好
    • 此代码可用于将 HTML 转换为 Attributed 和 Attributed 到 HTML,反之亦然。根据您的要求生成并不容易。你可以通过输入来检查上面的代码,这样你就会得到准确的输出。
    • 好的,谢谢。我会检查
    • 是的,当然... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2018-12-11
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多