在UILabel iphone中显示HTML文本
我从Web服务得到一个HTML响应下面是我得到的响应的HTML
<p><strong>Topic</strong>Gud mrng.</p> \n<p><strong>Hello Everybody</strong>: How are you.</p> \n<p><strong>I am fine</strong>: 1 what about you.</p>
我需要在UILabel中显示文本。
请帮忙
您可以使用属性文本在没有任何第三方库的情况下执行此操作。 我相信它确实接受HTML片段,就像你得到的那样,但是你可能想把它包装在一个完整的HTML文档中,以便你可以指定CSS:
static NSString *html = @"<html>" " <head>" " <style type='text/css'>" " body { font: 16pt 'Gill Sans'; color: #1a004b; }" " i { color: #822; }" " </style>" " </head>" " <body>Here is some <i>formatting!</i></body>" "</html>"; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; NSError *err = nil; label.attributedText = [[NSAttributedString alloc] initWithData: [html dataUsingEncoding:NSUTF8StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: &err]; if(err) NSLog(@"Unable to parse label text: %@", err);
不简洁,但你可以通过添加一个类别到UILabel来消除混乱:
@implementation UILabel (Html) - (void) setHtml: (NSString*) html { NSError *err = nil; self.attributedText = [[NSAttributedString alloc] initWithData: [html dataUsingEncoding:NSUTF8StringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: &err]; if(err) NSLog(@"Unable to parse label text: %@", err); } @end
…
[someLabel setHtml:@"Be <b>bold!</b>"];
从: https : //stackoverflow.com/a/5581178/237838
将HTML转换为纯文本下载文件
并使用
在你的NSString
上使用stringByConvertingHTMLToPlainText
函数
要么
您可以使用DTCoreText (以前称为HTML的NSAttributedString添加)。
这里是swift 2版本:
let htmlStringData = NSString(string: "<strong>Your HTML String here</strong>").dataUsingEncoding(NSUTF8StringEncoding) guard let html = htmlStringData else { return } do { let htmlAttrString = try NSAttributedString(data: html, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) yourLabel.attributedText = htmlAttrString } catch { print("An error occured") }
Swift 3:版本
extension String { func htmlAttributedString() -> NSAttributedString? { guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil } guard let html = try? NSMutableAttributedString( data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else { return nil } return html } }
Swift 2:版本
extension String { func htmlAttributedString() -> NSAttributedString? { guard let data = self.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false) else { return nil } guard let html = try? NSMutableAttributedString( data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else { return nil } return html } }
使用它作为:
label.attributedText = yourStringVar.htmlAttributedString()
Swift 3中的上述答案:
func htmlAttributedString() -> NSAttributedString? { guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil } guard let html = try? NSMutableAttributedString( data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) else { return nil } return html }
最近我一直在处理部分HTML片段,并将它们转换为具有添加属性的属性string。 这是我的扩展版本
import Foundation import UIKit extension String { func htmlAttributedString(attributes: [String : Any]? = .none) -> NSAttributedString? { guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return .none } guard let html = try? NSMutableAttributedString( data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: .none) else { return .none } html.setAttributes(attributes, range: NSRange(0..<html.length)) return html } }
我这样称呼它:
let attributes = [ NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName : UIFont.systemFont(ofSize: 12).traits(traits: .traitItalic) ] label?.attributedText = partialHTMLString.htmlAttributedString(attributes: attributes)