<b>标签在 UILabel 属性字符串中不起作用 [英] <b> tag not working in UILabel attributed string

查看:31
本文介绍了<b>标签在 UILabel 属性字符串中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 UILabel 但很少有标签在 UILabel 中不起作用,我会将 html 字符串显示为属性文本

I am displaying html string into attributed text if UILabel but few of the tags are not working in UILabel like

lineLabel.numberOfLines=0;
 NSString* html = self.offerArray[0][@"description"];
NSString * htmlString = [NSString stringWithFormat: @"%@%@</body></html>",kHTMLHeadOffer,html];
htmlString = [htmlString stringByReplacingPercentEscapesUsingEncoding:NSUnicodeStringEncoding];
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UIFont *font = [UIFont fontWithName:kFontName size:kOrderFont]; [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [attrStr length])];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrStr length])];
lineLabel.attributedText = attrStr;

推荐答案

请尝试此代码一次.

NSString *htmlstr = @"<p>This text is normal.</p> <p><b>This text is bold</b>.
</p> <p><big>Bigger text</big></p> <font size=3 color=red>This is some text!</font>
<font size=2 color=blue>This is some text!</font> <font face=verdana color=green>This is 
some text!</font>"; 

NSAttributedString *strAttributed = [[NSAttributedString alloc]
initWithData: [htmlstr dataUsingEncoding:NSUTF8StringEncoding] options: 
@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil
error: nil];

[_lblHtmlText setAttributedText:strAttributed];

这是输出屏幕.

这篇关于&lt;b&gt;标签在 UILabel 属性字符串中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆