gpt4 book ai didi

ios - 如何在UILabel上显示HTML字符串

转载 作者:行者123 更新时间:2023-12-01 17:29:40 41 4
gpt4 key购买 nike

我有一个UILabel。我想设置一个字符串,其中还包含一些HTML标记。字符串是:

NSString *str = @"<p>Hey you. My <b>name </b> is <h1> Joe </h1></p>";

我如何在 UILabel上显示它
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];



mylabel.attributedText=attrStr;

上面的代码不显示任何内容。

最佳答案

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

NSString * htmlString = @" <p><b>Hey</b> you. My <b>name </b> is <h1> Joe </h1></p> ";
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字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301145/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com