Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing to see if I can build an Opentype Panel
framework("CoreText");
const document = require("sketch").getSelectedDocument();
const textLayer = document.selectedLayers.layers[0];
// WORKS (System font has native support for small caps):
const font = NSFont.systemFontOfSize_weight(28, NSFontWeightBold);
// DOESN'T WORK: (Depending on if the existing font has small caps support:
// const font = textLayer.font;
// Two notes:
// 1. Had to look it up, [] in ES6 brackets can be used to use variables as object literal keys -
// before you were actually using "NSFontFeatureSettingsAttribute" instead of what that variable is set to, which is "CT"-something
// 2. kLowerCaseType & kLowerCaseSmallCapsSelector are defined in the CoreText framework
const descriptor = font.fontDescriptor().fontDescriptorByAddingAttributes({
[NSFontFeatureSettingsAttribute]: [{
[NSFontFeatureTypeIdentifierKey]: kLowerCaseType,
[NSFontFeatureSelectorIdentifierKey]: kLowerCaseSmallCapsSelector
}]
});
const newFont = NSFont.fontWithDescriptor_size(descriptor, 20);
// - Using _object here worked, but it's the private version of the public sketchObject property.
// Using _object or sketchObject gave you the MSTextLayer (internal Sketch Objective-C class)
// that the Sketch JS library Text object (textLayer) wraps.
// - The Text object has the font property so we _should_ be able to use that, but for whatever reason it's not working, so...
textLayer.sketchObject.setFont(newFont);
document.sketchObject.inspectorController().reload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment