Skip to content

Instantly share code, notes, and snippets.

@matt-curtis
Forked from KevinGutowski/FontSpike.js
Last active December 18, 2018 16:37
Show Gist options
  • Save matt-curtis/e2847ec5ebde98ac18a4d1253fff8970 to your computer and use it in GitHub Desktop.
Save matt-curtis/e2847ec5ebde98ac18a4d1253fff8970 to your computer and use it in GitHub Desktop.
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