I have index.html
and style.css
files. In these files I have only text. And I want to show this text in my textView
. If I use this code I show text from file:
class ReadViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
do {
guard let filePath = Bundle.main.path(forResource: "index", ofType: "html")
else {
print ("File reading error")
return
}
let contents = try String(contentsOfFile: filePath, encoding: .utf8)
textView.attributedText = contents.htmlToAttributedString
}
catch {
print ("File HTML error")
}
}
}
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
But in this case my text displayed without bold and other style settings. How to fix it? How to add style.css
file?