Joshua Joshua - 1 year ago 180
HTML Question

HTML from NSAttributedString

Rather than converting HTML to an attributed string, I need to convert it back to HTML. This can easily be done on Mac as can be seen here:

Unfortuately, the method

is only available on Mac via the
AppKit Additions.

My question is how can you do this on iOS?

Nik Nik
Answer Source

Not the 'easy' way, but what about iterating through the attributes of the string using:

- (void)enumerateAttributesInRange:(NSRange)enumerationRange 
                        usingBlock:(void (^)(NSDictionary *attrs, NSRange range, BOOL *stop))block

Have an NSMutableString variable to accumulate the HTML (lets call it 'html'). In the block, you would construct the HTML manually using strings. For instance if the text attributes 'attrs' specify red, bold text:

[html appendFormat:@"<span style='color:red; font-weight: bold;'>%@</span>", [originalStr substringWithRange:range]]

EDIT: Stumbled across this yesterday:

NSAttributedString+HTMLFromRange category from "UliKit" (

Looks like it will do what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download