Html с смайликами на струну Swift - PullRequest
1 голос
/ 24 апреля 2020

First UseCase: -

let htmlStringPara ="<p><strong>What is the 2019 novel coronavirus (COVID-19)?</strong><br /><br />It is a virus that causes a respiratory infection.<br /><strong>How you can protect yourself against the 2019 novel coronavirus:</strong><br /><br />These everyday tips below can help to prevent the spread, reduce exposure and protect your health:</p>

let StringPara =  htmlStringPara.html2String
print(StringPara)

What is the 2019 novel coronavirus (COVID-19)?

It is a virus that causes a respiratory infection.

How you can protect yourself against the 2019 novel coronavirus:

These everyday tips below can help to prevent the spread, reduce exposure and protect your health

Second UseCase:- 

let htmlStrng = "@@@@-/:;()$&amp;@“‘!?,.[]{}#%^*+=•£¥€&gt;<|\\_.,?!’??????????????????????zzzz />"
let convertHtmlString = htmlStrng.html2String
print(convertHtmlString)
//output 
"@@@@-/:;()$&@“‘!?,.[]{}#%^*+=•£¥€><|\\_.,?!’ðŸ˜ðŸ˜‚😂😂😀😌😞ðŸ˜ðŸ˜ðŸ˜ðŸ˜ðŸ˜ðŸ˜›ðŸ˜›ðŸ˜›ðŸš 🚠🚠😊😊😊😊zzzz />

Я действительно запутался, потому что я пытаюсь преобразовать html в строку и показать в текстовом представлении. В случае использования Frist HTML корректно преобразуется в строку, но во втором случае в html содержат смайлики или специальные чартеры не работают должным образом. Я использую приведенный ниже код для преобразования html в строку. Пожалуйста, дайте мне знать, что это правильно, если я ошибаюсь, пожалуйста, дайте мне знать правильный и лучший способ конвертировать html в строку и показать в текстовом виде.

var html2AttributedString: NSAttributedString? {
    do {
        return try NSAttributedString(data: Data(utf8), options: [NSAttributedString.DocumentReadingOptionKey.defaultAttributes : String.Encoding.utf8.rawValue,NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
    } catch {
        return nil
    }
}

var html2String: String {
    return html2AttributedString?.string ?? ""
}

Может кто-нибудь, пожалуйста, объясните мне, как покажите html, чтобы связать с эмодзи. Любая помощь будет принята с благодарностью.

Заранее спасибо.

...