ios-Swift:以标签或textVi显示HTML数据
我有一些HTML数据,其中包含标题,段落,图像和列表标签。
有没有一种方法可以在一个UITextView或UILabel中显示此数据?
12个解决方案
146 votes
对于Swift 4:
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
然后,每当您要将HTML文本放入UITextView时,请使用:
textView.attributedText = htmlText.htmlToAttributedString
Roger Carvalho answered 2019-11-15T18:40:49Z
34 votes
这是Swift 3版本:
private func getHtmlLabel(text: String) -> UILabel {
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.attributedString = stringFromHtml(string: text)
return label
}
private func stringFromHtml(string: String) -> NSAttributedString? {
do {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let d = data {
let str = try NSAttributedString(data: d,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return str
}
} catch {
}
return nil
}
我在这里找到其他一些答案的问题,花了我一些时间才能正确解决。 我设置了换行模式和行数,以便当HTML跨多行时标签的大小适当。
garie answered 2019-11-15T18:41:20Z
13 votes
添加此扩展名以将您的html代码转换为常规字符串:
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
然后在UITextView或UILabel中显示String
textView.text = yourString.html2String或
label.text = yourString.html2String
Himanshu answered 2019-11-15T18:41:56Z
7 votes
Swift 3.0
var attrStr = try! NSAttributedString(
data: "text".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
label.attributedText = attrStr
Ved Rauniyar answered 2019-11-15T18:42:15Z
7 votes
此后,我在更改文本属性时遇到问题,我可以看到其他人问为什么...
因此最好的答案是将扩展名与NSMutableAttributedString结合使用:
extension String {
var htmlToAttributedString: NSMutableAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSMutableAttributedString(data: data,
options: [.documentType: NSMutableAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
}
然后您可以通过以下方式使用它:
if let labelTextFormatted = text.htmlToAttributedString {
let textAttributes = [
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 13)
] as [NSAttributedStringKey: Any]
labelTextFormatted.addAttributes(textAttributes, range: NSRange(location: 0, length: labelTextFormatted.length))
self.contentText.attributedText = labelTextFormatted
}
Kassy Barb answered 2019-11-15T18:42:52Z
6 votes
我正在使用这个:
extension UILabel {
func setHTML(html: String) {
do {
let attributedString: NSAttributedString = try NSAttributedString(data: html.data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
self.attributedText = attributedString
} catch {
self.text = html
}
}
}
Nikolay Khramchenko answered 2019-11-15T18:43:11Z
4 votes
斯威夫特3
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = data(using: String.Encoding.utf8)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
Alex Morel answered 2019-11-15T18:43:30Z
2 votes
试试这个:
let label : UILable! = String.stringFromHTML("html String")
func stringFromHTML( string: String?) -> String
{
do{
let str = try NSAttributedString(data:string!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true
)!, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil)
return str.string
} catch
{
print("html error\n",error)
}
return ""
}
希望对您有所帮助。
Iyyappan Ravi answered 2019-11-15T18:43:55Z
1 votes
如果您想要HTML,图像和列表,则UILabel不支持此功能。 但是,我发现YYText可以解决问题。
Christopher Kevin Howell answered 2019-11-15T18:44:19Z
1 votes
在UITextView或UILabel中无法显示图像和文本段落,为此,您必须使用UIWebView。
只需将项目添加到情节提要中,链接到您的代码,然后调用它以加载URL。
OBJ-C
NSString *fullURL = @"http://conecode.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
迅速
let url = NSURL (string: "http://www.sourcefreeze.com");
let requestObj = NSURLRequest(URL: url!);
viewWeb.loadRequest(requestObj);
分步教程。[http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/]
Ulysses answered 2019-11-15T18:45:05Z
1 votes
上面的答案在这里是Swift 4.2
extension String {
var htmlToAttributedString: NSAttributedString? {
guard
let data = self.data(using: .utf8)
else { return nil }
do {
return try NSAttributedString(data: data, options: [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
Stephen Chen answered 2019-11-15T18:45:31Z
-1 votes
如果您具有内含HTML代码的字符串,则可以使用:
extension String {
var utfData: Data? {
return self.data(using: .utf8)
}
var htmlAttributedString: NSAttributedString? {
guard let data = self.utfData else {
return nil
}
do {
return try NSAttributedString(data: data,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
} catch {
print(error.localizedDescription)
return nil
}
}
var htmlString: String {
return htmlAttributedString?.string ?? self
}
}
并以您的代码使用:
label.text = "something".htmlString
Pedro Menezes answered 2019-11-15T18:46:01Z