HTML格式在UITextView [英] HTML Format in UITextView
问题描述
我从Web上解析数据并获取这种格式的字符串:
F& uuml; r更多信息请点击
我已经尝试了一个UILabel,但经过一些研究之后,我现在经常使用UITextView。在归属检查器中,我将其设置为归因文本,并启用了链接检测。现在文本显示为红色,可点击。
现在的问题是,HTML标记和正确的(德语)字符集仍然丢失,我不知道如何显示在右边
所显示的字符串是以这种方式分析的:
func showHTMLString(unformattedString:String) - > NSAttributedString {
var attrStr = NSAttributedString(
data:tmpString.dataUsingEncoding(NSUnicodeStringEncoding,allowLossyConversion:true)!,
options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
documentAttributes:nil,
错误:无)
返回attrStr!
$ b 如果我用 attrStr?.string填充Textview
格式以正确的方式显示,但链接也没有了。
如何以正确的方式格式化显示的字符串?
在此先感谢
AR4G4
解决方案问题是你必须改变字符编码选项从NSUnicodeStringEncoding NSUTF8StringEncoding加载你的HTML适当的方式。我认为你应该创建一个字符串扩展只读计算属性来将您的HTML代码转换为属性字符串:
Xcode 8.3.1•Swift 3.1
扩展数据{
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字符串{
var data:Data {
return Data utf8)
$ b let htmlStringCode =F& uuml; r mehr Informationing klicken sie< a href = \http://www.samplelink.com/subpage.php?id=8 \\ >此处< / A> 中
htmlStringCode.data.attributedString?.string ?? //Fürmehr Informationen klicken sie here
在你的情况下
yourTextView.attributedText = htmlStringCode.data.attributedString
i'm quite new to iOS Development and right now working on an app which receive some kind of JSON Data. But some Backend Experts thought, that it would be better for the User if they just copy the Information straight out of Word and paste it into the information System. So I'm sitting here, trying to make a clickable Link in a UITableView.
I parse the data from Web and get a String with this format:
Für mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>.
I tried already a UILabel, but after some research I use now the often suggested UITextView. In the Attributed Inspector, i set it as an Attributed Text and enabled the Link Detection. Now the text is shown red and is clickable.
The Problem now for me is, that the HTML Tags and the correct (German) Character Set is still missing and i got no idea, how to display it in the right way.
The shown string is parsed in this way:
func showHTMLString(unformattedString: String) -> NSAttributedString{
var attrStr = NSAttributedString(
data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
return attrStr!
}
If i fill the Textview with attrStr?.string
the Format is shown in the correct way but the link is gone as well.
Any suggestions how to format the shown string in the right way?
Thanks in advance
AR4G4
解决方案 The problem there is that you have to change the Character Encoding options from NSUnicodeStringEncoding to NSUTF8StringEncoding to load your of your html the proper way. I think you should create a string extension read-only computed property to convert your html code to attributed 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ü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"
in your case
yourTextView.attributedText = htmlStringCode.data.attributedString
这篇关于HTML格式在UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!