如何在 iOS 的 UILabel 上显示 HTML 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19946251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show an HTML string on a UILabel in iOS?
提问by vasudev
I am getting a title in HTML format as
我得到一个 HTML 格式的标题
<p><span style="color: #000000;"><strong>Example </strong></span></p>
<p><span style="color: #000000;"><strong>示例</strong></span></p>
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.
我需要在 UILabel 中显示这个 HTML 字符串。颜色代码和字体大小应与 HTML 中的相同。当我将 HTML 字符串转换为 NSString 时,只有文本“示例”出现,而不是颜色。
Is there any solution?
有什么解决办法吗?
Thnx in advance
提前谢谢
Till now I am trying by using a NSAttributedString in following way but by this way the whole HTML is printing:
到目前为止,我正在尝试通过以下方式使用 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;
回答by sanjana
For iOS7 or more you can use this:
对于 iOS7 或更高版本,您可以使用它:
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;
回答by BaseZen
Swift 2
斯威夫特 2
let htmlText = "<p>etc</p>"
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) ")
}
}
Swift 3
斯威夫特 3
let htmlText = "<p>etc</p>"
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) ")
}
}
回答by Jakub Truhlá?
Swift 4+
斯威夫特 4+
For Swift 4 and above use:
对于 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)
回答by mak
I did this on UITextView as follows:
我在 UITextView 上这样做了,如下所示:
[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];
Or you can use RTLabel library: https://github.com/honcheng/RTLabelto display html text along with its formatting on a label.
或者您可以使用 RTLabel 库:https: //github.com/honcheng/RTLabel在标签上显示 html 文本及其格式。
回答by Orkhan Alikhanov
Extension for Swift 4+
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)")
}
}
}
}
回答by Adarsh G J
-(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];
回答by Adarsh G J
If Html Text is like
如果 Html 文本就像
var planeText = "Unseen and not Purchased!"
//setting color to the text
var htmlText = "<font color=\"#f20707\">" + planeText + "</font>"
We can set the text to UILabel
like this
我们可以将文字设置成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) ")
}
}