如何在UILabel中的文本下方显示

While <em>La La Land </em>is a film emphasising the problems

我尝试使用下面的代码,但与指定字体一起使用时,它没有显示<em>标记(斜体显示)内的文本正确格式。
NSMutableAttributedString *attrHTMLText = [[NSAttributedString alloc] initWithData:[yourHTMTextHere dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
[attrHTMLText addAttribute:NSFontAttributeName value:yourLabel.font range:NSMakeRange(0, attrHTMLText.length)];
[attrHTMLText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attrHTMLText.length)];
Label.attributedText = attrHTMLText;

最佳答案

如果要使文本的某些部分变为斜体,则可以使用NSMutableAttributedString

NSMutableAttributedString *strText = [[NSMutableAttributedString alloc] initWithString:@"While La La Land is a film emphasising the problems"];
[strText addAttribute:NSFontAttributeName
              value:[UIFont fontWithName:@"Helvetica-Italic" size:22]
              range:NSMakeRange(7, 16)];
[label setAttributedText: strText];
来实现

10-08 07:46