NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[stringFromHtml dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil]; self.textLabel.attributedText = attrString; self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0]; 

The \r\n , \t and <br> function understands, but bold <b> </b> and italic <i> </i> - refuses to understand (ignores).

  • why not manually parse bold and italik? - Max Mikheyenko
  • Your code worked fine for me. can change the encoding try to UTF8 - Max Mikheyenko
  • Changed ... bold text does not highlight, with Italik the same trouble - Chekist
  • I understand it is impossible without third-party libraries to do this in the original UILabel? - Chekist

1 answer 1

here is this line

 self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0]; 

overrides all your <b> and <i> because ios uses the fonts for text styles.

To change the font, use the category: https://gist.github.com/funkydevil/aff16a6c94dff283960b

Here is an example:

 -(void)testFunction { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,50,self.view.frame.size.width,30)]; [self.view addSubview:label]; NSString *myString = @"<b>BOLD</b> <i>ITALIC</i> REGULAR"; NSMutableAttributedString *attributedString; attributedString = [[[NSMutableAttributedString alloc] initWithData:[myString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil] mutableCopy]; [attributedString setFontButSaveStyle:[UIFont fontWithName:@"HelveticaNeue-Light" size:30.0]]; label.attributedText = attributedString; }