HTML格式在UITextView

我对iOS开发很陌生,现在正在开发一种接收某种JSON数据的应用程序。 但是一些后端专家认为,如果用户直接从Word中拷贝信息并将其粘贴到信息系统中,会对用户更好。 所以我坐在这里,试图在UITableView中创build一个可点击的链接。

我parsing来自Web的数据并获取这种格式的string:

F&uuml;r mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>. 

我已经尝试过一个UILabel,但经过一些研究,我现在使用的是经常使用的UITextView。 在“归属检查器”中,我将其设置为“归因文本”,并启用了链接检测。 现在文本显示为红色,可点击。

现在的问题是,HTML标签和正确的(德语)字符集仍然是缺less的,我不知道,如何以正确的方式显示它。

显示的string是这样parsing的:

  func showHTMLString(unformattedString: String) -> NSAttributedString{ var attrStr = NSAttributedString( data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil) return attrStr! } 

如果我使用attrStr?.string填充attrStr?.string ,格式以正确的方式显示,但链接也不见了。

任何build议如何格式化正确的方式显示的string?

在此先感谢AR4G4

问题是你必须改变字符编码选项从NSUnicodeStringEncoding NSUTF8StringEncoding加载你的HTML适当的方式。 我认为你应该创build一个string扩展只读的计算属性来将您的HTML代码转换为属性string:

Xcode 8.3.1•Swift 3.1

 extension Data { var attributedString: NSAttributedString? { do { return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print(error) } return nil } } extension String { var data: Data { return Data(utf8) } } 

 let htmlStringCode = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>" htmlStringCode.data.attributedString?.string ?? "" // "Für mehr Informationen klicken sie here" 

在你的情况

 yourTextView.attributedText = htmlStringCode.data.attributedString 

我会build议在UIWebView显示HTML。 它比使用UITextView更强大。 请参阅在uitextview中显示html文本以获取更多信息。

在IB中检查你的UITextView的属性。 为了使链接正常工作,您必须选中Selectable

在这里输入图像说明

创build了你的属性string之后,你可以设置UITextViewattributedText NSAttributedString属性为NSAttributedString本身,而不是属性string的string属性。

你的版本非常接近开始。 正如莱昂纳多·萨维奥·达布斯(Leonardo Savio Dabus)所说,你应该尝试NSUTF * StringEncoding。 以下为我产生您的预期输出。 正如他所说的,如果你这样做的话,你可能想把它添加到string的扩展中。

  let theString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." let theAttributedString = NSAttributedString(data: str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil) theTextView.attributedText = atString 

我曾经这样做的另一种方式:

 var someHtmlString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.CaseInsensitive]) let range = NSRange(location: 0, length: someHtmlString.characters.count) let htmlLessString: String = regex.stringByReplacingMatchesInString(someHtmlString, options: NSMatchingOptions(), range:range, withTemplate: "") 

最终结果 – > htmlLessString是

 "F&uuml;r mehr Informationen klicken sie here." 

我有一个应用程序有一个UITextView,我想要能够从浏览器粘贴HTML格式的文本,然后将其保存为一个string(包含HTML格式)到数据库,然后再次从数据库中检索它并显示它与第一次从网站复制的格式相同。 我通过这两个扩展来pipe理这个:

 extension String { func getAttributedStringFromHTMLString() -> NSAttributedString { do { let attributedString = try NSAttributedString(data: self.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil) return attributedString } catch { print(error) return NSAttributedString() } } } extension NSAttributedString { func getHTMLString() -> String { var htmlText = ""; let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let htmlData = try self.dataFromRange(NSMakeRange(0, self.length), documentAttributes:documentAttributes) if let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) { htmlText = htmlString } return htmlText } catch { print("error creating HTML from Attributed String") return "" } } } 

我使用Swift 4的代码:

 var descriptionStr : String = String() //Dynamic text let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.caseInsensitive]) let range = NSRange(location: 0, length: descriptionStr.count) let htmlLessString: String = regex.stringByReplacingMatches(in: descriptionStr, options: NSRegularExpression.MatchingOptions(), range:range, withTemplate: "") textViewRef.text = htmlLessString