Created
June 16, 2020 11:22
-
-
Save janily/583cc81588e1acf9281d2675885844f5 to your computer and use it in GitHub Desktop.
text is selected update font properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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