简体   繁体   English

NSAttributedString更改字体大小

[英]NSAttributedString changing the font size

I am using this extension of String to get proper attributed text from the HTML tags in a String. 我正在使用String的这个扩展来从String中的HTML标记中获取正确的属性文本。

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 . 我在UICollectionView中使用UILabel的值。

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. 但默认情况下它带有大小为11的“Times New Roman”字体。所以我想让它变得更大。 I tried using NSMutableAttributedString . 我尝试使用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. 如果我直接增加UILabel的字体大小,那么字体大小会增加但是斜体属性不起作用。

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

Please help me out here to make the String little bigger. 请帮我在这里使String变大。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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