hzxu hzxu - 5 months ago 16
iOS Question

NSString: strip out <b></b> and make an attributed string with color for that segment?

Say if I have a string:

This is a < b >simple < /b > string.

I need to get rid of the < b >, (sorry there is no space between b and angle bracket, for some reason the preview does not show it), also make the word 'simple' to be bold, my thought was:

  1. replace the angle brackets and br with empty space

  2. make the 'simple' segment to have attributes

The problem is once the tags are removed, I still need to know the word's location, do I first remember the location of 'simple', after removal, the location-4 should be the new location of 'simple'? Is there any better way? Or even transform html tag to attributes?


Should be b instead of br


There is API available in iOS 7 that makes this very easy. It will convert an NSString of (possible) HTML text to an NSAttributedString.

NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType };

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithData:[myHTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];

It will even preserve any in-line CSS applied, even background-color!

Note that if no font and font size is specified in the HTML text, the default will be Times New Roman 12. You could specify it in this fashion: <style>body { font-family:-apple-system; font-size:14px; }<style>. If you do not specify the font via CSS, you can still override the font, but you will need to manually handle bold, italics, etc otherwise that formatting will be lost if you set the font for the entire string. One approach is to enumerateAttribute: NSFontAttributeName on the mutable attributed string looking for 'bold' etc in the font name, and if it's found then replace that range with the desired font, such as the user's preferred font and size but the bold etc version of it, and continue replacing fonts with each range obtained from the enumeration.