简体   繁体   中英

NSAttributedString changing the font size

I am using this extension of String to get proper attributed text from the HTML tags in a String.

extension String {

    var html2AttributedString: NSAttributedString? {
        guard
            let data = dataUsingEncoding(NSUTF8StringEncoding)
            else { return nil }
        do {
            return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding ], documentAttributes: nil)

        } catch let error as NSError {
            print(error.localizedDescription)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

And I am using the value in a UILabel inside a UICollectionView .

if let value = mainNote.html2AttributedString
{
    cell.note.attributedText = value
}
else
{
    cell.note.text = mainNote
}

This method works great. But by default it comes with the "Times New Roman" font with size of 11. So I wanted to make it little bigger. I tried using NSMutableAttributedString .

if let value = mainNote.html2AttributedString
{
    let mutableString = NSMutableAttributedString(string: value.string, attributes: [NSFontAttributeName : UIFont(name: "Times New Roman", size: 20)!])
    cell.note.attributedText = mutableString
}
else
{
    cell.note.text = mainNote
}

Which actually did nothing.

If I increase the font size of UILabel directly then the font size gets increased but the italic attribute does not work.

cell.note.font = UIFont(name: "Times New Roman", size: 16)

Please help me out here to make the String little bigger.

Use this updated extension:

extension String {

func html2AttributedString(font: UIFont?) ->  NSAttributedString? {
    guard
        let data = dataUsingEncoding(NSUTF8StringEncoding)
        else { return nil }
    do {

        let string = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil)

        let newString = NSMutableAttributedString(attributedString: string)
        string.enumerateAttributesInRange(NSRange.init(location: 0, length: string.length), options: .Reverse) { (attributes : [String : AnyObject], range:NSRange, _) -> Void in
                if let font = font {
                    newString.removeAttribute(NSFontAttributeName, range: range)
                    newString.addAttribute(NSFontAttributeName, value: font, range: range)
                }
        }
        return newString

    } catch let error as NSError {
        print(error.localizedDescription)
        return  nil
    }
}
var html2String: String {
    return html2AttributedString(nil)?.string ?? ""
}

}

Usage:

if let value = mainNote.html2AttributedString(UIFont(name: "Times New Roman-ItalicMT", size: 20))
{
    cell.note.attributedText = value
}
else
{
    cell.note.text = mainNote
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM