如何在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;
答案 0 :(得分:0)
如果您想将文本的某些部分设为itallic,那么您可以使用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];