Hi everyone,
I’m building a Sketch plugin that needs to apply rich text with different colors to text layers programmatically. I need to parse HTML and apply inline styles (colors, fonts, etc.) to different segments of text.
What I’m trying to achieve: Set text like “this is red, this is blue” where each part has a different color applied. (This is just a basic thing i want to get to work, the end goal is to set more styling props like font size, weight etc)
**What I’ve tried:
- Creating NSMutableAttributedString with attributes:**
const mutaString = NSMutableAttributedString.alloc().init()
mutaString.replaceCharactersInRange_withString_(NSMakeRange(0, 0), plainString)
// Apply attributes using Sketch’s keys
mutaString.addAttribute_value_range_(“MSAttributedStringColorAttribute”, redColor, redRange)
mutaString.addAttribute_value_range_(“MSAttributedStringFontAttribute”, currentFont, redRange)
This works fine - the attributed string is created successfully.
2. Setting it on the layer - this is where it fails:
const msTextLayer = textLayer.sketchObject
msTextLayer.setAttributedString_(mutaString) // ❌ Causes Obj-C exception
What doesn’t work:
-
msTextLayer.setAttributedString_() - Objective-C exception
-
MSAttributedString.alloc().initWithString_() - method doesn’t exist
-
MSAttributedString.alloc().initWithAttributedString_() - crashes
-
msAttribStr.mutableCopy() - Objective-C exception (when called on the result of msTextLayer.attributedString())_
Questions:
-
What’s the correct way to set an attributed string on an MSTextLayer via the Mocha bridge?
-
Should I be using
MSColorinstead of NSColor for the color attributes? -
Is there a different method signature or approach for setAttributedString that works?
-
Is there an alternative API I should be using instead?
I found a snippet showing how to read attributes from an MSAttributedString, but I can’t find working examples of how to write them back.
Any help would be greatly appreciated!

