1

I'm looking to convert an NSAttributedString into HTML, but only preserve bold and italic attributes. I don't care about font family, font size, or anything like that.

I'm basically looking to convert: Go Falcons

to

<b>Go</b> <em>Falcons</em>

Is there a way to do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
wrdswrds
  • 169
  • 5
  • 1
    There's nothing built-in for this. You will have to write your own code to handle it. – rmaddy Oct 15 '15 at 21:54
  • This could be helpfull : http://stackoverflow.com/questions/5298188/how-do-i-convert-nsattributedstring-into-html-string – Rached Anis Oct 15 '15 at 22:57

1 Answers1

2

I am not sure if this can be a viable solution but one way to do this is to enumerate the attributes and go through the each segment and make your own HTML.

 [str enumerateAttribute:NSFontAttributeName
                inRange:range
                options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
             usingBlock:^(id value, NSRange range, BOOL *stop) {
               UIFont *currentFont = value;
               if ([currentFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch]
                       .location != NSNotFound) {
                   //Do something with the string and range for bold? add tags and append in a different string
               } 
               // Similarly do something for for non-bold, itatlic or normal text. Keep Appending them in a string
             }];
insanoid
  • 413
  • 2
  • 10
  • This is the right general idea. But instead of looking at the font name, get the font's attributes and check its weight to see if the font is bold. – rmaddy Oct 16 '15 at 03:55