Swift:填充AutoreleasingUnsafeMutablePointer

问题描述 投票:1回答:2

我是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
swift ios8 dtcoretext
2个回答
5
投票

此时您不应该使用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,因为在我真正关心的第二个词典中什么也没回来。


1
投票

要更新生成的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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.