简体   繁体   English

在swift中将HTML + Unicode更改为字符串

[英]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.
  }

and the result is 结果是 在此输入图像描述

if u can see there's weird symbol around the text. 如果你能看到文本周围有奇怪的符号。 Can someone help me with this? 有人可以帮我弄这个吗?

Use SwiftSoup 使用SwiftSoup

Exemple 为例

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()
  • unclosed tags (eg <p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p> ) 未封闭的标签(例如, <p>Lorem <p>Ipsum解析为<p>Lorem</p> <p>Ipsum</p>
  • implicit tags (eg a naked <td>Table data</td> is wrapped into a <table><tr><td>... ) 隐式标签(例如裸<td>Table data</td>被包装到<table><tr><td>...
  • reliably creating the document structure ( html containing a head and body , and only appropriate elements within the head) 可靠地创建文档结构(包含headbody html ,以及头部内仅适当的元素)

The object model of a document 文档的对象模型

  • Documents consist of Elements and TextNodes 文档由Elements和TextNodes组成
  • The inheritance chain is: Document extends Element extends Node.TextNode extends Node . 继承链是: Document extends Element extends Node.TextNode extends Node
  • An Element contains a list of children Nodes, and has one parent Element. 元素包含子节点列表,并具有一个父元素。 They also have provide a filtered list of child Elements only. 它们还仅提供子元素的筛选列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM