首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在“快速3”中显示文本视图中的属性文本?

在“快速3”中显示文本视图中的属性文本?
EN

Stack Overflow用户
提问于 2017-06-06 10:47:23
回答 2查看 3.8K关注 0票数 0

我想用意大利语显示文本,粗体是从服务器接收的。

< b> hello < /i >< /b >world< /i >< /p > so,

代码语言:javascript
运行
复制
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
    }

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-08 12:33:31

编辑:更新Swift 4

extension创建一个String

代码语言:javascript
运行
复制
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
    }
}

在项目中的任何地方调用它:

代码语言:javascript
运行
复制
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上设置字符串,请使用:

代码语言:javascript
运行
复制
textView.attributedText = textToBeConverted.htmlAttributedString() ?? ""

结果:

票数 4
EN

Stack Overflow用户

发布于 2017-06-06 10:57:43

从下面的函数中将属性文本分配给UITextView,

代码语言:javascript
运行
复制
//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")
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44387939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档