NSLayoutManager.drawGlyphs() can not display MarkDown attributed string

We use NSLayoutManager/NSTextStorage/NSTextContainer to display markdown attributed string on a canvas. MarkDown string was processed properly, but NSLayoutManager just exhibits the plain text. The code is as follows:

    var layoutmanager = NSLayoutManager()
    var textstorage = NSTextStorage()
    var textcontainer = NSTextContainer()
 
    var attrStr = try AttributedString.init(markdown: "**test**", options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))
    var attrString = NSMutableAttributedString(attrStr)     
    self.textstorage.setAttributedString(attrString!)

// draw text layout
    let range = self.layoutmanager.glyphRange(for: self.textcontainer)
    self.layoutmanager.drawBackground(forGlyphRange: range, at: self.textLocation)
    self.layoutmanager.drawGlyphs(forGlyphRange: range, at: self.textLocation)

Is it because TextKit 1 does not support markdown string display or I miss something else ?

Any help will be appreciated. Thanks!

Accepted Reply

I made a sample project to show how I solved this:

https://github.com/frankrausch/AttributedStringStyledMarkdown

  • Great, it works for me. Thanks!

Add a Comment

Replies

I made a sample project to show how I solved this:

https://github.com/frankrausch/AttributedStringStyledMarkdown

  • Great, it works for me. Thanks!

Add a Comment