【问题标题】:NSFontAttributeName not applied to NSAttributedStringNSFontAttributeName 未应用于 NSAttributedString
【发布时间】:2016-04-05 13:13:59
【问题描述】:

我目前正在使用此answer 提供的此扩展程序将html 转换为UITextView 中的字符串。一切正常,但由于某些奇怪的原因,NSFontAttributeName 在此过程中未应用于字符串。我在这里做错了什么吗? (或者我应该在查看属性化的 html 解析字符串后应用 NSAttributedString 吗?如果是这样,是否可以将属性应用到 NSAttributeString?)。

PS。使用html2String 时字体大小没有变化,但UITextView 可以识别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, NSFontAttributeName : UIFont.systemFontOfSize(17.0)], documentAttributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
cell.desc.attributedText  = apiData[currentIndex].description!.html2AttributedString //Font attribute not recognized
cell.desc.text  = apiData[currentIndex].description!.html2String //Font recognized by links no longer recognized

【问题讨论】:

  • 这是正常行为。 NSFontAttributeName 不是initWithData:options:documentsAttributes:error:options 参数的允许键。您必须在之后重写字体效果。因此,要么通过将 HTML 添加到原始字符串中来应用它,要么在之后应用它(这样可能会删除粗体/斜体效果)。
  • @Larme 感谢您提供这些详细信息,我不知道此方法中忽略了此属性。如果他们决定像这样转换,我将把答案留给 OP。

标签: ios swift nsattributedstring


【解决方案1】:

我不确定为什么(可能是因为有一个范围),但如果你先创建一个 NSMutableAttributedString 然后添加 UIFont 属性,它就可以工作:

extension String {

    var html2AttributedString: NSMutableAttributedString? {
        guard
            let data = dataUsingEncoding(NSUTF8StringEncoding)
            else { return nil }
        do {
            let attrStr = try NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
            attrStr.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)], range: NSRange(location: 0, length: attrStr.length))
            return attrStr
        } catch let error as NSError {
            print(error.localizedDescription)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

【讨论】:

  • 一点警告:如果 HTML 部分有粗体或斜体效果,像这样设置字体会删除它(因为它在 UIFont/NSFontAttributeName 部分中)。它不起作用的原因是NSFontAttributeName 在该初始化方法中不是有效的键(并且被忽略)。
  • 非常感谢 Eric! @Larme我不会设置任何粗体或斜体效果,这个答案适合我的需要。感谢您的信息
  • @kye 不客气。 :) 并再次感谢 Larme 提供的其他信息。
  • @EricAya 完美运行。感谢您的精彩回答!
【解决方案2】:

这里我已经更新到 SWIFT 3

extension String {

    var utf8Data: Data? {
        return data(using: .utf8)
    }
}

extension Data {
    var attributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue,NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize: 15)], documentAttributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
        return nil
    }

    var attributedStringHtml: NSMutableAttributedString? {
        do {
         let   attrStr = try NSMutableAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue], documentAttributes: nil)
            attrStr.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 17.0),NSForegroundColorAttributeName:UIColor.white], range: NSRange(location: 0, length: attrStr.length))
            return attrStr
        } catch let error as NSError {
            print(error.localizedDescription)
        }
        return nil
    }
}

用法

 DispatchQueue.global(qos: .background).async {
            let attribut = self.sampleText.utf8Data?.attributedStringHtml
            DispatchQueue.main.async {
                self.convertedTextLbl.attributedText = attribut
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多