[英]iOS - copy all NSMutableAttributedString attributes to another NSMutableAttributedString
[英]Get all attributes of a NSMutableAttributedString into a dictionary
我有一个NSMutableAttributedString,其中包含不同范围的不同属性。 我想在字典中读取具有属性的所有范围,以便可以通过Api将所有属性保存到Core Data和MySql中,然后再次重建属性字符串。
这是我的属性字符串
let myString = "P is for Pizza and Pizza is for me"
//: Initialize the mutable string
let myMutableString = NSMutableAttributedString(string: myString,attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
//: Make the first Pizza in chalkduster 24 point
myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Chalkduster",size: 24.0)!,range: NSRange(location: 9,length: 5))
//: Make the second pizza red and outlined in Helvetica Neue
myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Helvetica Neue",size: 36.0)!,range: NSRange(location: 19,length: 5))
myMutableString.addAttribute(NSStrokeColorAttributeName,value: UIColor.red,range: NSRange(location: 19,length: 5))
myMutableString.addAttribute(NSStrokeWidthAttributeName,value: 4,range: NSRange(location: 19,length: 5))
//: Set the background color is attributes text.
//: which is not the color of the background text.
let stringLength = myString.characters.count
myMutableString.addAttribute(NSBackgroundColorAttributeName,value: UIColor.magenta,range: NSRange(location: 0,length: stringLength))
//:Change to 48 point Menlo
myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Menlo",size: 48.0)!,range: NSRange(location: 27,length: 7))
在执行以下代码
let attributes = attributedText.attributes(at: 0, longestEffectiveRange: nil, in: NSRange(location: 0, length: attributedText.length))
print(attributes)
得到以下输出
["NSColor": UIExtendedSRGBColorSpace 0 0 1 1, "NSBackgroundColor": UIExtendedSRGBColorSpace 1 0 1 1, "NSFont": <UICTFont: 0x7fff12000110> font-family: "American Typewriter"; font-weight: bold; font-style: normal; font-size: 36.00pt]
打印myMutableString.description时出现以下字符串
print(myMutableString.description)
P is for {
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
}Pizza{
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90cd32a80> font-family: \"Chalkduster\"; font-weight: normal; font-style: normal; font-size: 24.00pt";
} and {
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
}Pizza{
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90cd32d90> font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 36.00pt";
NSStrokeColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSStrokeWidth = 4;
} is{
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90cc36e20> font-family: \"Georgia\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
} for me{
NSBackgroundColor = "UIExtendedSRGBColorSpace 1 0 1 1";
NSFont = "<UICTFont: 0x7fd90f8406d0> font-family: \"Menlo-Regular\"; font-weight: normal; font-style: normal; font-size: 48.00pt";
}
转换成html
var s: NSAttributedString? = "Pizza stirng"
var documentAttributes: [AnyHashable: Any] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
var htmlData: Data? = try? s?.data(from: NSRange(location: 0, length: s?.length), documentAttributes: documentAttributes as? [String : Any] ?? [String : Any]())
var htmlString = String(data: htmlData, encoding: String.Encoding.utf8)
并尝试以此取回NSAttributedString
var atributedStr = try? NSAttributedString(data: htmlString.data(using: String.Encoding.utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: (String.Encoding.utf8)], documentAttributes: nil)
Swift 3.0扩展
extension NSAttributedString {
convenience init(htmlString:String){
do {
try self.init(data: htmlString.data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
print(error.localizedDescription)
}
}
func toHtml(location:Int,length:Int) -> String {
let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
let range = NSRange.init(location: location, length: length)
do {
let htmlData = try self.data(from: range, documentAttributes: documentAttributes)
let htmlString = String(data: htmlData, encoding: .utf8)
return htmlString!
} catch {
return error.localizedDescription
}
}
}
用法:
let myString = "P is for Pizza and Pizza is for me"
//: Initialize the mutable string
let myMutableString = NSMutableAttributedString(string: myString,attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
//: Make the first Pizza in chalkduster 24 point
myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Chalkduster",size: 24.0)!,range: NSRange(location: 9,length: 5))
//: Make the second pizza red and outlined in Helvetica Neue
myMutableString.addAttribute(NSFontAttributeName,value: UIFont(name: "Helvetica Neue",size: 36.0)!,range: NSRange(location: 19,length: 5))
//: Create Html String from NSMutableAttributedString
let htmlString = myMutableString.toHtml(location: 0, length: myMutableString.string.length);
print(htmlString)
//: Create NSMutableAttributedString from HTML String
let newAttributedString = NSMutableAttributedString(htmlString: htmlString)
print(newAttributedString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.