如何在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:@"
或者您可以使用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