获取NSMutableAttributedString的所有属性到字典中

时间:2017-07-17 06:24:56

标签: ios swift nsattributedstring nsmutableattributedstring

我有一个NSMutableAttributedString,它包含不同范围的不同属性。我想在字典中读取所有带有属性的Ranges,以便我可以通过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))

myMutableString output

执行以下代码

 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";
 }

2 个答案:

答案 0 :(得分:1)

转换为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)

答案 1 :(得分:1)

Swift 3.0扩展

 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)

用法:

addition