如何在iOS中的UILabel上显示HTML字符串? [英] How to show an HTML string on a UILabel in iOS?

查看:369
本文介绍了如何在iOS中的UILabel上显示HTML字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取HTML格式的标题

I am getting a title in HTML format as

< p>< span style =" color:#000000;">< strong> ;示例< / strong>< / span>< / p>

<p><span style="color: #000000;"><strong>Example </strong></span></p>

我需要在UILabel中显示此HTML字符串。颜色代码和字体大小应与HTML中的相同。当我将HTML字符串转换为NSString时,只会出现文本Example,而不是颜色。

I need to show this HTML string in a UILabel. The color code and the font size should be same as the coming in HTML. When I am converting the HTML string into a NSString, only the text "Example" is coming, and not the color.

有没有解决方案?

Thnx提前

直到现在我正在尝试通过以下方式使用NSAttributedString,但通过这种方式整个HTML正在打印:

Till now I am trying by using a NSAttributedString in following way but by this way the whole HTML is printing:

UIFont *font = [UIFont systemFontOfSize:14.0];
UIFont *secondFont = [UIFont systemFontOfSize:10.0];

NSMutableDictionary *firstAttributes;
NSMutableDictionary *secondAttributes;

NSDictionary *firstAttributeFont = @{NSFontAttributeName:font};
NSDictionary *secondAttributeFont = @{NSFontAttributeName:secondFont};

[firstAttributes addEntriesFromDictionary:firstAttributeFont];
[secondAttributes addEntriesFromDictionary:secondAttributeFont];

[firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];
[secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];



NSString* completeString = [NSString stringWithFormat:@"%@",strTitle];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]     initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:strTitle]];
//   [attributedString setAttributes:secondAttributes range:[completeString rangeOfString:self.secondAttributeText]];
Cell.lbl_Title.attributedText = attributedString;


推荐答案

对于iOS7或更高版本,您可以使用:

For iOS7 or more you can use this:

NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;

这篇关于如何在iOS中的UILabel上显示HTML字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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