Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Last active January 27, 2019 02:19
Show Gist options
  • Save KevinGutowski/aec04d4545cb18c1fbb30445768ab752 to your computer and use it in GitHub Desktop.
Save KevinGutowski/aec04d4545cb18c1fbb30445768ab752 to your computer and use it in GitHub Desktop.
Attempting to set font attributes during selection
const Sketch = require('sketch')
framework("CoreText");
let myTextLayer = Sketch.getSelectedDocument().selectedLayers.layers[0]
let textLayer = myTextLayer.sketchObject
if (textLayer.isEditingText() == 0) {
console.log("select some text")
} else {
let textStorage = myTextLayer.sketchObject.editingDelegate().textStorage()
// Todo: Figure out how to get the selected range / range of the text object
let myRange = NSMakeRange(0,5)
let myFont = textLayer.font()
let lowerCaseFont = updateFontWithID_Key(myFont, kLowerCaseType,kLowerCaseSmallCapsSelector)
textStorage.beginEditing()
textStorage.addAttribute_value_range(NSFontAttributeName,lowerCaseFont,myRange)
textStorage.fixAttributesInRange(myRange)
textStorage.endEditing()
}
function updateFontWithID_Key(font, key, value) {
let settingsAttribute = getSettingsAttributeForKey_value(key, value)
const descriptor = font.fontDescriptor().fontDescriptorByAddingAttributes(settingsAttribute)
const newFont = NSFont.fontWithDescriptor_size(descriptor,font.pointSize())
return newFont
}
function getSettingsAttributeForKey_value(key, value) {
let settingsAttribute = {
[NSFontFeatureSettingsAttribute]: [{
[NSFontFeatureTypeIdentifierKey]: key,
[NSFontFeatureSelectorIdentifierKey]: value
}]
}
return settingsAttribute
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment