common
common

Reputation: 49

is there any function to make touchable url while converting html text to plain text

I'm trying to convert HTML text into plain text but the URL present in HTML in not touchable in plain text

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)

Upvotes: 1

Views: 69

Answers (2)

AtulParmar
AtulParmar

Reputation: 4570

You can just go through UITextView for simple plain text with touchable.

Set UITextView property Link and Selectable is true, See the following.

Assign simple plain text to 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

Upvotes: 0

AamirR
AamirR

Reputation: 12218

Here is how you convert 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

That yields:

enter image description here

Upvotes: 1

Related Questions