HTML特殊键显示在帖子描述中

时间:2016-07-17 08:24:05

标签: ios swift

我有一个带帖子单元格的UITableView,在描述中到处都有空格,到处都是这样的东西:

Hello,


Text goes here   and more here   and even
more  

和换行符。这是一个示例帖子:

Hello, Text goes here and more here and even
more. And we have room for this now because 
we don't have the line breaks.

注意大规模的断线,很奇怪&amp ;;事物和随机的三四个空格。所以我的问题是如何使描述看起来像这样:

{{1}}

如果我们能够像邮件应用程序描述一样,那就太棒了。

谢谢,

1 个答案:

答案 0 :(得分:2)

试试这个解决方案。

ASCII_file

//调用此方法转换字符串

extension String{

  func decodeEnt() -> String{
    let encodedData = self.dataUsingEncoding(NSUTF8StringEncoding)!
    let attributedOptions : [String: AnyObject] = [
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
    ]
    var attributedString = NSAttributedString()

    do
    {

     attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
    }
    catch
    {
        print("error")
    }

    return attributedString.string
}