objective c - Cocoa: How to save NSAttributedString to JSON -


i have nsattributedstring object property of custom object. need save custom object disk in json format. later need send json data across network java server.
can't use -(nsstring) string method of nssattributedstring object because need able reconstruct attributed string off disk , on server.

nsattributedstring has 2 properties:

  • the string
  • an array of attribute "runs"

each "run" has:

  • an integer range applies to
  • a dictionary of key/value attributes

it easy represent json, using enumerateattributesinrange:options:usingblock:.

something like:

{   "string" : "hello world",   "runs" : [     {       "range" : [0,3],       "attributes" : {         "font" : {           "name" : "arial",           "size" : 12         }       }     },     {       "range" : [3,6],       "attributes" : {         "font" : {           "name" : "arial",           "size" : 12         },         "color" : [255,0,0]       }     },     {       "range" : [9,2],       "attributes" : {         "font" : {           "name" : "arial",           "size" : 12         }       }     }   ] } 

edit: here's example implementation:

// create basic attributed string nsmutableattributedstring *attstr = [[nsmutableattributedstring alloc] initwithstring:@"hello world" attributes:@{nsfontattributename: [nsfont fontwithname:@"arial" size:12]}]; [attstr addattribute:nsforegroundcolorattributename value:[nscolor redcolor] range:nsmakerange(3, 6)];  // build array of attribute runs nsmutablearray *attributeruns = [nsmutablearray array]; [attstr enumerateattributesinrange:nsmakerange(0, attstr.length) options:0 usingblock:^(nsdictionary *attrs, nsrange range, bool *stop) {   nsarray *rangearray = @[[nsnumber numberwithunsignedinteger:range.location],                           [nsnumber numberwithunsignedinteger:range.length]];    nsmutabledictionary *runattributes = [nsmutabledictionary dictionary];   [attrs enumeratekeysandobjectsusingblock:^(id attributename, id attributevalue, bool *stop) {      if ([attributename isequal:nsfontattributename]) { // convert font values dictionary name , size       attributename = @"font";       attributevalue = @{@"name": [(nsfont *)attributevalue displayname],                          @"size": [nsnumber numberwithfloat:[(nsfont *)attributevalue pointsize]]};      } else if ([attributename isequaltostring:nsforegroundcolorattributename]) { // convert foreground colour values array red/green/blue number 0 255       attributename = @"color";       attributevalue = @[[nsnumber numberwithinteger:([(nscolor *)attributevalue redcomponent] * 255)],                          [nsnumber numberwithinteger:([(nscolor *)attributevalue greencomponent] * 255)],                          [nsnumber numberwithinteger:([(nscolor *)attributevalue bluecomponent] * 255)]];      } else { // skip unknown attributes       nslog(@"skipping unknown attribute %@", attributename);       return;     }       [runattributes setobject:attributevalue forkey:attributename];   }];    // save attributes (if there any)   if (runattributes.count == 0)     return;    [attributeruns addobject:@{@"range": rangearray,                              @"attributes": runattributes}]; }];  // build json output nsdictionary *jsonoutput = @{@"string": attstr.string,                              @"runs": attributeruns}; nsdata *jsondata = [nsjsonserialization datawithjsonobject:jsonoutput options:nsjsonwritingprettyprinted error:null];  nslog(@"%@", [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]); exit(0); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -