网站制作公司 云南,艺术设计类网站,手机推广平台,三亚文明城市建设服务中心报名网站ios-Swift#xff1a;以标签或textVi显示HTML数据我有一些HTML数据#xff0c;其中包含标题#xff0c;段落#xff0c;图像和列表标签。有没有一种方法可以在一个UITextView或UILabel中显示此数据#xff1f;12个解决方案146 votes对于Swift 4#xff1a;extension Stri…ios-Swift以标签或textVi显示HTML数据我有一些HTML数据其中包含标题段落图像和列表标签。有没有一种方法可以在一个UITextView或UILabel中显示此数据12个解决方案146 votes对于Swift 4extension 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.htmlToAttributedStringRoger Carvalho answered 2019-11-15T18:40:49Z34 votes这是Swift 3版本private func getHtmlLabel(text: String) - UILabel {let label UILabel()label.numberOfLines 0label.lineBreakMode .byWordWrappinglabel.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:20Z13 votes添加此扩展名以将您的html代码转换为常规字符串extension String {var html2AttributedString: NSAttributedString? {guardlet 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中显示StringtextView.text yourString.html2String或label.text yourString.html2StringHimanshu answered 2019-11-15T18:41:56Z7 votesSwift 3.0var attrStr try! NSAttributedString(data: text.data(using: String.Encoding.unicode, allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],documentAttributes: nil)label.attributedText attrStrVed Rauniyar answered 2019-11-15T18:42:15Z7 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:52Z6 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:11Z4 votes斯威夫特3extension String {var html2AttributedString: NSAttributedString? {guardlet 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:30Z2 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:55Z1 votes如果您想要HTML图像和列表则UILabel不支持此功能。 但是我发现YYText可以解决问题。Christopher Kevin Howell answered 2019-11-15T18:44:19Z1 votes在UITextView或UILabel中无法显示图像和文本段落为此您必须使用UIWebView。只需将项目添加到情节提要中链接到您的代码然后调用它以加载URL。OBJ-CNSString *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:05Z1 votes上面的答案在这里是Swift 4.2extension String {var htmlToAttributedString: NSAttributedString? {guardlet 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.htmlStringPedro Menezes answered 2019-11-15T18:46:01Z