How to set the html string to the textView iOS swift
Solution:
You can easily get the html string and set that to textview.In textview it had an option called attributed text to set the string with attributes like bold ,italic ,underline etc ..
The below code is used for set the html properties based text to the textView
Code:
You can easily get the html string and set that to textview.In textview it had an option called attributed text to set the string with attributes like bold ,italic ,underline etc ..
The below code is used for set the html properties based text to the textView
Code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myTxtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
var html = "<b><i>text</i></b>"
myTxtView.attributedText = html.htmlToAttrString
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension String {
var htmlToAttrString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToText: String {
return htmlToAttrString?.string ?? ""
}
}
Comments
Post a Comment