Rajat Jain
Rajat Jain

Reputation: 1022

Get all attributes of a NSMutableAttributedString into a dictionary

I have a NSMutableAttributedString which contains different attributes for different ranges. I want to read all Ranges with attributes in a dictionary so that I can save all attributes into Core Data and MySql via Api and then again reconstruct the attributed string.

Here is my attributed string

    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

on executing the following code

 let attributes = attributedText.attributes(at: 0, longestEffectiveRange: nil, in: NSRange(location: 0, length: attributedText.length))

 print(attributes)

getting the following output

 ["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]

I am getting the following string when I am printing 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";
 }

Upvotes: 2

Views: 3444

Answers (2)

Rajat Jain
Rajat Jain

Reputation: 1022

Swift 3.0 Extension

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

Usage:

 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)

Upvotes: 1

Convert to 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)

and try this to get back NSAttributedString

 var atributedStr = try? NSAttributedString(data: htmlString.data(using: String.Encoding.utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: (String.Encoding.utf8)], documentAttributes: nil)

Upvotes: 1

Related Questions