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

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.


The current answer is OK, and I have +1'd it. Yet it was only a clue, not a real solution. If you are looking for a solution to the OP's question, take a look here.

You should focus on the following:

First implement these methods:

- (NSString *)styledHTMLwithHTML:(NSString *)HTML {
    NSString *style = @"<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>";

    return [NSString stringWithFormat:@"%@%@", style, HTML];
}

- (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML {
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
    return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL];
}

Later use them like that:

// This is a string that you might find in your model
NSString *html = @"This is <b>bold</b>";

// Apply some inline CSS
NSString *styledHtml = [self styledHTMLwithHTML:html];

// Generate an attributed string from the HTML
NSAttributedString *attributedText = [self attributedStringWithHTML:styledHtml];

// Set the attributedText property of the UILabel
label.attributedText = attributedText;