Search code examples
objective-cipadios4

UILabel text as html text


I am stuck with a small issue.

I need to use a sentence which will have first two words bold and last two words italic. Like:

I am using an Objective C Developer.

How to do that. Is this possible in Objective C?


Solution

  • For iOS7 you can use this:

    NSString * htmlString = @"<html><body> Some html string </body></html>";
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    UILabel * myLabel = [UILabel alloc] init];
    myLabel.attributedText = attrStr;