如何在iOS中的UILabel上显示HTMLstring?
我正在以HTML格式获得一个标题
<p> <span style =“color:#000000;”> <strong>示例</ strong> </ span> </ p>
我需要在UILabel中显示这个HTMLstring。 颜色代码和字体大小应与HTML中的相同。 当我将HTMLstring转换为NSString时,只有文本“Example”即将出现,而不是颜色。
有没有解决办法?
Thnx提前
直到现在我正在尝试通过以下方式使用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;
对于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;
Swift 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
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) ") } }
我在UITextView上做了如下:
[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];
或者,您可以使用RTLabel库: https : //github.com/honcheng/RTLabel在标签上显示html文本及其格式。
-(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];
Swift 3的扩展
extension UILabel { func from(html: String) { if let htmlData = html.data(using: String.Encoding.unicode) { do { self.attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) } catch let e as NSError { print("Couldn't parse \(html): \(e.localizedDescription)") } } } }
- “项目名称”是用优化编译的 – 步进可能performance怪异; variables可能不可用
- UIViewContentModeScaleAspectFit和UIViewContentModeScaleToFill之间的区别?
- 是否有可能在没有Mac的情况下为iOS创buildPhoneGap应用程序?
- 未捕获的exception:这个类不是关键值编码兼容的
- UITableview:如何禁用某些行而不是其他的select
- 使用Facebook ios SDK在应用程序中注销Facebook
- 如何以编程方式在UINavigationController中按“返回”button
- iPhone上的纵向UISplitViewController显示细节VC而不是主
- UIButton标题alignment和多行支持