【问题标题】:add multiple attributes to NSAttributedString and encode HTML向 NSAttributedString 添加多个属性并编码 HTML
【发布时间】:2018-06-06 09:32:02
【问题描述】:

我想将文本编码为 HTML 并同时应用多个属性。

我的 Swift 4 代码如下所示:

let attributedString = try? NSMutableAttributedString(
                data: tweetContent.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
                options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
                documentAttributes: nil)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.4

let attributes = [NSAttributedStringKey.font: UIFont(name: "Roboto-Regular", size: 17.0)!,
                NSAttributedStringKey.paragraphStyle: paragraphStyle]

let textRangeForFont : NSRange = NSMakeRange(0, attributedString!.length)
attributedString?.addAttributes(attributes, range: textRangeForFont)

tweetContent 表示我要为其设置字体并且应该为 HTML 编码的字符串。最后,我想在不丢失 html 编码的情况下为该字符串设置尽可能多的属性。

但是,此代码会导致显示我在情节提要中设置的占位符文本。

感谢任何帮助!

【问题讨论】:

    标签: text encode nsattributedstring


    【解决方案1】:

    我忘记将属性文本添加到 textview:

    textView.attributedText = attributedString 
    

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多