In my code there s an extension for NSAttributedString
:
internal convenience init?(html: String) {
guard let data = html.data(using: String.Encoding.utf8, allowLossyConversion: true) else {
return nil
}
print(UIKit.Thread.isMainThread) //TRUE
guard let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) else { //here is the error
return nil
}
self.init(attributedString: attributedString)
}
then I try to use it like this:
let text = "<p>Your order has been created. </p><p>Below You can find the details of Your order:</p><p>Order ID: 183</p><p>Summary: <ul><li>Filtered coffee 50.00 x 1</li></ul></p><p>Service fee: 30.0</p><p>Total: 80.0 Kn</p><p>You will receive a message when our staff starts preparing Your order.<br/></p>"
let attributedString = NSAttributedString(html: text)
text has the following value:
Your order has been created.
Below You can find the details of Your order:
Order ID: 183
Summary:
- Filtered coffee 50.00 x 1
Service fee: 30.0
Total: 80.0 Kn
You will receive a message when our staff starts preparing Your order.
What is wrong?;)
EDIT:
I use it with MessageKit to display attributed text:
extension Message: MessageType {
var sender: Sender {
return Sender(id: createdBy?.identifier ?? "", displayName: createdBy?.name ?? "BOT_RESPONSE")
}
var messageId: String {
return identifier
}
var sentDate: Date {
return date
}
var kind: MessageKind {
guard let attributedString = NSAttributedString(html: text) else {
return .text(text)
}
return .attributedText(attributedString)
}
}
When I put the breakpoint at line where error arise and print it on console:
po NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
then everything is fine;) Why?