[英]Change HTML+Unicode to string in swift
I want to change the api return html+unicode to string in TextView. 我想在TextView中将api返回html + unicode更改为字符串。 But i notice there's weird symbol.
但我注意到有奇怪的符号。 And i dont know how can i change that to appropriate symbol.
我不知道如何将其改为适当的符号。 Right now im using this extension :
现在我使用这个扩展名:
extension String {
var html2String:NSAttributedString {
return try! NSAttributedString(data:data(using: String.Encoding.utf8, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
and my current Code is : 我目前的守则是:
override func viewDidLoad() {
super.viewDidLoad()
setupWhiteLeftButton()
//self.navigationItem.title = "About".capitalized
let htmlString = str.html2String
txtView.attributedText = htmlString
txtView.font = UIFont(name: "Play", size: 14.0)
txtView.tintColor = UIColor(red: 228/255, green: 49/255, blue: 23/255, alpha: 1.0)
txtView.isEditable = false;
txtView.scrollsToTop = true
let contentHeight = txtView.contentSize.height
let offSet = txtView.contentOffset.x
let contentOffset = contentHeight - offSet
txtView.contentOffset = CGPoint(x: 0, y: -contentOffset)
// Do any additional setup after loading the view.
}
if u can see there's weird symbol around the text. 如果你能看到文本周围有奇怪的符号。 Can someone help me with this?
有人可以帮我弄这个吗?
To parse a HTML document: 要解析HTML文档:
let html = "<html><head><title>First parse</title></head><body><p>Parsed HTML into a doc.</p></body></html>"
let doc: Document = try SwiftSoup.parse(html)
let myText = try doc.text()
<p>Lorem <p>Ipsum
parses to <p>Lorem</p> <p>Ipsum</p>
) <p>Lorem <p>Ipsum
解析为<p>Lorem</p> <p>Ipsum</p>
) <td>Table data</td>
is wrapped into a <table><tr><td>...
) <td>Table data</td>
被包装到<table><tr><td>...
) html
containing a head
and body
, and only appropriate elements within the head) head
和body
html
,以及头部内仅适当的元素) Document
extends Element
extends Node.TextNode
extends Node
. Document
extends Element
extends Node.TextNode
extends Node
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.