Swift: 填充 AutoreleasingUnsafeMutablePointer<NSDictionary?>

Swift: fill AutoreleasingUnsafeMutablePointer<NSDictionary?>

我是 DTCoreText,负责将 HTML 转换为属性文本。因为我想预先设置字体,而不是之后设置字体,因为那样会覆盖所有粗体、斜体等标签,所以我想在构造函数中设置文档属性。这个构造函数要我提供一个 AutoreleasingUnsafeMutablePointer,它或多或少似乎是一个 NSDictionary?在前面。有点。只是它不允许我以任何方式设置它。我试过 .memory,尝试以任何可能的方式转换字典,但它不接受任何数据。

    let font = UIFont.systemFontOfSize(12)
    let data = info.desc?.dataUsingEncoding(NSUTF8StringEncoding)
    let attributes: NSMutableDictionary? = NSMutableDictionary()
    attributes!.setObject(font, forKey: NSFontAttributeName)
    var attributeRef: AutoreleasingUnsafeMutablePointer<NSDictionary?> = AutoreleasingUnsafeMutablePointer.null()
    NSMutableAttributedString(HTMLData: data, documentAttributes: nil)
    //attributeRef = *attributeDict
    let attributedString = NSMutableAttributedString(HTMLData: data, documentAttributes:attributeRef)
    let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping;
    let range = NSMakeRange(0, attributedString.length)
    attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
    lblMessage.attributedText = attributedString

此时您不应该使用 DTCoreText; iOS 现在有对此的原生调用。只需说 var dict = NSDictionary?() 并传递 &dict。这是示例代码:

let s = "<html><body><h1>Howdy</h1><p>Hello</p></body></html>"
let d = s.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false)
var dict = NSDictionary?()
let att = NSAttributedString(data: d!, options: [
    NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType
    ], documentAttributes: &dict, error: nil)
println(att!)
println(dict!)

您会发现这非常有效。这里是 dict:

BottomMargin = 72;
Converted = "-1";
DocumentType = NSHTML;
LeftMargin = 90;
PaperMargin = "UIEdgeInsets: {72, 90, 72, 90}";
PaperSize = "NSSize: {612, 792}";
RightMargin = 90;
TopMargin = 72;
UTI = "public.html";

但是,我通常会通过 nil,因为在我真正关心的第二个字典中没有返回任何内容。

要更新生成的 HTML 中的字体,请使用类似于以下的代码:

Swift 5

extension String {

    /// Convert HTML to NSAttributedString
    func convertHtml() -> NSAttributedString {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
            let string = NSMutableAttributedString(attributedString: attributedString)

            // Apply text color
            string.addAttributes([.foregroundColor: UIColor.text], range: NSRange(location: 0, length: attributedString.length))

            // Update fonts
            let regularFont = UIFont(name: Fonts.Regular, size: 13)! // DEFAULT FONT (REGUALR)
            let boldFont = UIFont(name: Fonts.Bold, size: 13)! // BOLD FONT
            /// add other fonts if you have them

            string.enumerateAttribute(.font, in: NSMakeRange(0, attributedString.length), options: NSAttributedString.EnumerationOptions(rawValue: 0), using: { (value, range, stop) -> Void in

                /// Update to our font
                // Bold font
                if let oldFont = value as? UIFont, oldFont.fontName.lowercased().contains("bold") {
                    string.removeAttribute(.font, range: range)
                    string.addAttribute(.font, value: boldFont, range: range)
                }
                // Default font
                else {
                    string.addAttribute(.font, value: regularFont, range: range)
                }
            })
            return string
        }
        return NSAttributedString()
    }
}