wrdswrds
wrdswrds

Reputation: 169

Convert NSAttributedString to HTML with only bold and italics

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?

Upvotes: 1

Views: 616

Answers (1)

insanoid
insanoid

Reputation: 413

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

Upvotes: 2

Related Questions