Я пытаюсь преобразовать HTML-текст в обычный текст, но URL-адрес, присутствующий в HTML, недоступен для простого текста

var htmlString = """
<p style=\"text-align: justify;\">The Ministry of Corporate Affairs (MCA) has informed vide Flash Alert that Form AGILE is likely to be revised on MCA21 Company Forms Download page with effect from May 31, 2019.&nbsp;</p>\n<p style=\"text-align: justify;\">Form AGILE is an application for Goods and services tax Identification number, employees state Insurance corporation registration plus Employees provident fund organisation registration. &nbsp;</p>\n<p style=\"text-align: justify;\">Stakeholders are advised to check the latest version before filing.</p>\n<p style=\"text-align: justify;\"><a href=\"http://www.mca.gov.in/ /
"""
let encodedData = htmlString.data(using: .unicode, allowLossyConversion: false)


var attributedString: NSAttributedString?

do {
    attributedString = try NSAttributedString(data: encodedData!, options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html,NSAttributedString.DocumentReadingOptionKey.characterEncoding:NSNumber(value: String.Encoding.utf8.rawValue)], documentAttributes: nil)

} catch let error as NSError {
    print(error.localizedDescription)
} catch {
    print("error")
}
print(encodedData!)
print(attributedString)
1
Sanju 31 Май 2019 в 09:57

2 ответа

Лучший ответ

Вот как вы конвертируете HTML

var htmlString = """
<p style=\"text-align: justify;\">The Ministry of Corporate Affairs (MCA) has informed vide Flash Alert that Form AGILE is likely to be revised on MCA21 Company Forms Download page with effect from May 31, 2019.&nbsp;</p><p style=\"text-align: justify;\"><a href=\"http://example.org\">Link</a></p>
"""

let attributedString = try? NSAttributedString(data: Data(htmlString.utf8), options: [
    .documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
textView.attributedText = attributedString

Это дает:

enter image description here

1
AamirR 31 Май 2019 в 07:08

Вы можете просто пройти UITextView для простого простого текста с сенсорным.

Задайте для свойства UITextView Link и Selectable значение true, см. Следующее.

Назначьте простой простой текст UITextView.

enter image description here

enter image description here

let htmlString = """
The Ministry of Corporate Affairs (MCA) has informed vide Flash Alert that Form AGILE is likely to be revised on MCA21 Company Forms Download page with effect from May 31, 2019.

Form AGILE is an application for Goods and services tax Identification number, employees state Insurance corporation registration plus Employees provident fund organisation registration.

Stakeholders are advised to check the latest version before filing.

http://www.mca.gov.in
"""
        txtView.text = htmlString
0
AtulParmar 31 Май 2019 в 07:18