Я пытаюсь преобразовать 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. </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. </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)
Вот как вы конвертируете 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. </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
Это дает:
ссылка не ведет на веб-страницу
Если вы установите textView.isEditable = false
, ссылка будет открываться при нажатии
Вы можете просто пройти UITextView
для простого простого текста с сенсорным управлением.
Установите UITextView
свойство Link
и Selectable
равно true
. См. следующее.
Назначьте простой текст для UITextView
.
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
«URL присутствует в HTML и не доступен для редактирования в обычном тексте» Определите «обычный текст». Вы используете
UILabel
?UITextView
? Как вы устанавливаетеattributedString
к нему?