If you want to change the baseline, of the selected part of the font you will need to drop down to the MacOS APIs and not use the Sketch Javascript API.
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let selection = doc.selectedLayers.layers[0]
try {
let textView = selection.sketchObject.editingDelegate().textView()
let textStorage = textView.textStorage()
let selectedRange = textView.selectedRange()
textStorage.addAttribute_value_range(NSBaselineOffsetAttributeName, 20, selectedRange)
textView.didChangeText()
} catch(e) {
console.log(e)
console.log("not editing")
}
Alright lets break this down
selection
is a Sketch JS API object of type Text
.
Text {
type: 'Text',
id: 'E487510D-70BF-4B56-ACC5-F313D477C2F8',
frame: { x: 164, y: 154, width: 147, height: 48 },
name: 'hi, Sketch',
...
}
Under the hood this text object is a custom Sketch object called MSTextLayer
(link to header)
console.log(selection.sketchObject)
// <MSTextLayer: 0x7fa7b8bc17b0>
When you go into the text editing mode you are no longer editing the text layer but rather a NSTextView
superimposed right on the spot of the Sketch object.
let textView = selection.sketchObject.editingDelegate().textView()
This can error out if you aren’t editing which is why we wrap it in a try/catch
.
Next, we need to get the range of the selection
let textStorage = textView.textStorage()
let selectedRange = textView.selectedRange()
Now we are ready to apply our font transformation using the selectedRange
We apply the text transformation to the textStorage
(not the textView
) and we notify the textView that we modified some properties so that the interface properly updates.
textStorage.addAttribute_value_range(NSBaselineOffsetAttributeName, 20, selectedRange)
textView.didChangeText()
Manipulating the line height is a bit harder so let me know if you are wanting to do that still.