在不更改html格式的情况下增加NSMutabelAttributedString的字体大小

时间:2018-07-17 11:30:28

标签: ios swift nsmutableattributedstring

有来自服务器的html字符串,我需要在UILabel中显示它们。我创建了一个扩展名来格式化html字符串中的文本。它工作正常,但textsize很小,我只想增加文本大小而不更改文本格式。

例如Minimum <b>50 characters</b><br>in ABOUT

“至少有 50个字符
关于ABOUT”

在不增加字体大小的情况下,我得到了正确的输出 但是当我设置字体大小时,它会松开粗体部分,输出变为

“最少50个字符”

extension UILabel{
    func set(htmlString: String){
        guard let data = htmlString.data(using: .utf8) else {return}
        guard let copy = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) else {return}
        let raw = copy.mutableCopy() as? NSMutableAttributedString
        raw?.addAttribute(.foregroundColor, value: self.textColor, range: NSRange.init(location: 0, length: copy.string.count))
        let alignment = NSMutableParagraphStyle()
        alignment.alignment = .center
        raw?.addAttribute(.paragraphStyle, value: alignment, range: NSRange.init(location: 0, length: copy.string.count))
        raw?.addAttribute(.font, value: UIFont.systemFont(ofSize: 20), range: NSRange.init(location: 0, length: copy.string.count))
        self.attributedText = raw
    }
}

0 个答案:

没有答案