1

The idea is to to turn html parts like <b> from json string that contains html At this moment I have the following code.

cell

cell.comment?.attributedStringValue = try! NSAttributedString(htmlString: currentPost.comment!)

extension

extension NSAttributedString {

    convenience init(htmlString html: String) throws {
        try self.init(data: Data(html.utf8), options: [
            .documentType: NSAttributedString.DocumentType.html,
            .characterEncoding: String.Encoding.utf8.rawValue
            ], documentAttributes: nil)
    }

}

I only found examples for ios. I tried experimenting with the following code with no luck NSAttributedString(string: quote, attributes: attributes)

let attributes = [NSAttributedStringKey.font:NSFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor: NSColor.white] as! [NSAttributedStringKey: Any]

UPDATE I rushed saying the problem was solved in the comments. The following code updates the font and font color. But it doesn't convert html parts into formatted text.

        let attributes = [NSAttributedStringKey.font:NSFont(name: "Verdana", size: 13)!,
                          NSAttributedStringKey.foregroundColor: NSColor.red] as [NSAttributedStringKey: Any]

        cell.comment?.attributedStringValue = try! NSAttributedString(string: currentPost.comment!, attributes: attributes)
kirqe
  • 2,431
  • 4
  • 37
  • 63

0 Answers0