Swift:填充自动释放非可变指针<;NSDictionary?>;
我使用DTCoreText将HTML转换为属性文本。因为我想在前面设置字体,而不是在后面,因为那样会覆盖所有粗体、斜体等标记,所以我想在构造函数中设置文档属性。这个构造器想让我给出一个或多或少像是NSDictionary的AutoReleasingUnsafemtablePointer?前面有&个。某种程度上。只是它不允许我以任何方式设置它。我试过。内存,试过以任何可能的方式转换字典,但它不接受任何数据Swift:填充自动释放非可变指针<;NSDictionary?>;,swift,ios8,dtcoretext,Swift,Ios8,Dtcoretext,我使用DTCoreText将HTML转换为属性文本。因为我想在前面设置字体,而不是在后面,因为那样会覆盖所有粗体、斜体等标记,所以我想在构造函数中设置文档属性。这个构造器想让我给出一个或多或少像是NSDictionary的AutoReleasingUnsafemtablePointer?前面有&个。某种程度上。只是它不允许我以任何方式设置它。我试过。内存,试过以任何可能的方式转换字典,但它不接受任何数据 let font = UIFont.systemFontOfSize(12)
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
let font=UIFont.systemFontOfSize(12)
让数据=info.desc?.dataUsingEncoding(NSUTF8StringEncoding)
let属性:NSMutableDictionary?=NSMutableDictionary()
属性!。setObject(字体,forKey:NSFontAttributeName)
var attributeRef:autoreleasingusafmtablepointer=autoreleasingusafmtablepointer.null()
NSMutableAttributeString(HTMLData:data,DocumentAttribute:nil)
//attributeRef=*attributeDict
让attributedString=NSMutableAttributedString(HTMLData:data,documentAttributes:attributeRef)
让paragraphStyle:NSMutableParagraphStyle=NSMutableParagraphStyle()
paragraphStyle.lineBreakMode=NSLineBreakMode.ByWordWrapping;
let range=NSMakeRange(0,attributedString.length)
attributedString.addAttribute(NSParagraphStyleAttributeName,值:paragraphStyle,范围: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!)
但是,我通常通过
nil
,因为我真正关心的第二本词典中没有任何内容返回。此时不应使用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!)
但是,我通常通过
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()
}
}
要更新生成的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()
}
}
您是否知道不再需要DTCoreText?iOS现在可以让你直接将HTML转换为NSAttributedString。谢谢matt!不幸的是,本机调用仍然需要相同的AutoreleasingUnsafemtablepointer:(只需说
var dict=NSDictionary?()
和pass&dict
。或nil
,如果您不需要生成的字典。我的误解是,我认为字典可以设置任何内容,但它不会。它只会返回。因此,在这种情况下它是无用的,但我无论如何都会尝试它,以供将来参考。您知道DTCoreText不再需要了吗?iOS现在允许您直接将HTML转换为NSAttributedString。谢谢matt!不幸的是,本机调用仍然需要相同的AutoReleasingUnsafemtablePointer:(只需说var dict=NSDictionary?()
和pass&dict
。或nil
如果您不需要生成的字典。我的误解是,我以为字典会设置任何内容,但它不会。它只会返回。因此,在这种情况下它是无用的,但我还是会尝试它,仅供将来参考。注意,这不会将HTML的默认字体设置为默认字体OP正在询问。NSAttributedString
API对HTML不这样做,所以大多数人在将字体规范扔到NSAttributedString
::。@rickster或者您可以形成属性字符串,然后在样式运行中循环更改字体。@departmentob我相信我已经回答过了d您提出的基本问题,即如何处理自动删除非对称指针
。是的,您没有时间实现它。从现在起5小时内尝试:)请注意,这并没有像OP所要求的那样为HTML设置默认字体。NSAttributedString
API不会为HTML设置默认字体,因此大多数人只会在HTML/CSS中添加字体规范,然后再将其扔到NSAttributedString
:。@rickster或者您可以形成属性字符串,然后循环使用样式runs正在更改字体。@departmentob我相信我已经回答了您提出的基本问题,即如何处理自动删除非对称指针。
。是的,您没有时间实现它。从现在起5小时内尝试:)