【发布时间】: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