html label显示特殊符号,如何在iOS中的UILabel上显示HTML字符串?

如何在iOS中的UILabel上显示HTML字符串?

我正在以HTML格式获得标题

示例 strong> span> p>

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

有什么解决办法吗?

提前致谢

到目前为止,我正在尝试通过以下方式使用NSAttributedString,但是通过这种方式,整个HTML都可以打印:

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;

vasudev asked 2020-01-30T17:50:17Z

7个解决方案

125 votes

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

NSString * htmlString = @"

Some html string ";

NSAttributedString * attrStr =

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]

options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}

documentAttributes:nil error:nil];

UILabel * myLabel = [[UILabel alloc] init];

myLabel.attributedText = attrStr;

sanjana answered 2020-01-30T18:04:02Z

28 votes

迅捷2

let htmlText = "

etc

"

if let htmlData = htmlText.dataUsingEncoding(NSUnicodeStringEncoding) {

do {

someLabel.attributedText = try NSAttributedString(data: htmlData,

options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],

documentAttributes: nil)

} catch let e as NSError {

print("Couldn't translate \(htmlText): \(e.localizedDescription) ")

}

}

迅捷3

let htmlText = "

etc

"

if let htmlData = htmlText.data(using: String.Encoding.unicode) {

do {

let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

} catch let e as NSError {

print("Couldn't translate \(htmlText): \(e.localizedDescription) ")

}

}

BaseZen answered 2020-01-30T18:04:27Z

16 votes

迅捷4+

对于Swift 4及更高版本,请使用:

guard let data = "foo".data(using: String.Encoding.unicode) else { return }

try? titleLabel.attributedText =

NSAttributedString(data: data,

options: [.documentType:NSAttributedString.DocumentType.html],

documentAttributes: nil)

Jakub Truhlář answered 2020-01-30T18:04:51Z

6 votes

我在UITextView上这样做如下:

[detailView loadHTMLString:[NSString stringWithFormat:@"

%@",[model.dict valueForKey:@"description"]] baseURL:nil];

或者您可以使用RTLabel库:[https://github.com/honcheng/RTLabel]以在标签上显示html文本及其格式。

mak answered 2020-01-30T18:05:16Z

4 votes

-(NSString*)convertHtmlPlainText:(NSString*)HTMLString{

NSData *HTMLData = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:HTMLData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:NULL error:NULL];

NSString *plainString = attrString.string;

return plainString;

}

UILabel * htmlLabel = [UILabel alloc] init];

htmlLabel.text = [self convertHtmlPlainText:htmlResponse];

Adarsh G J answered 2020-01-30T18:05:33Z

4 votes

Swift 4+的扩展

extension UILabel {

func set(html: String) {

if let htmlData = html.data(using: .unicode) {

do {

self.attributedText = try NSAttributedString(data: htmlData,

options: [.documentType: NSAttributedString.DocumentType.html],

documentAttributes: nil)

} catch let e as NSError {

print("Couldn't parse \(html): \(e.localizedDescription)")

}

}

}

}

Orkhan Alikhanov answered 2020-01-30T18:05:53Z

0 votes

如果HTML文本就像

var planeText = "Unseen and not Purchased!"

//setting color to the text

var htmlText = "" + planeText + ""

我们可以像这样将文本设置为UILabel

if let htmlData = htmlText.data(using: String.Encoding.unicode) {

do {

let attributedText = try NSAttributedString(data: htmlData,

options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],

documentAttributes: nil)

//Setting htmltext to uilable

uilable.attributedText = attributedText

} catch let e as NSError {

//setting plane text to uilable cause of err

uilable.text = planeText

print("Couldn't translate \(htmlText): \(e.localizedDescription) ")

}

}

answered 2020-01-30T18:06:17Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值