OGeek|极客世界-中国程序员成长平台

标题: html - 使用指定字体在 UILabel 中显示 html 标签 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:06
标题: html - 使用指定字体在 UILabel 中显示 html 标签

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

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

我尝试使用下面的代码,但是当与指定字体一起使用时,它没有显示 标签内的正确文本格式(斜体渲染)。

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;



Best Answer-推荐答案


如果你想让文本的某些部分变成斜体,你可以使用 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];

关于html - 使用指定字体在 UILabel 中显示 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505536/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4