Skip to content

Instantly share code, notes, and snippets.

@nolastan
Created November 23, 2015 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolastan/81a95b414548fb073cc5 to your computer and use it in GitHub Desktop.
Save nolastan/81a95b414548fb073cc5 to your computer and use it in GitHub Desktop.
var loadTypography = function (newStyles, sharedStyles) {
var alignmentHash = {
'left': 0,
'right': 1,
'center': 2,
'justified': 3
};
// removeAllStyles();
for(var i=0; i<newStyles.length; i++) {
createStyle(newStyles[i]);
}
function createStyle(style) {
if(style.Style == "") { return; }
var textLayer = [[MSTextLayer alloc] initWithFrame:nil];
if("Size" in style) { textLayer.setFontSize(style.Size); }
if("Line" in style) { textLayer.setLineSpacing(style.Line); }
if("Character" in style) { textLayer.setCharacterSpacing(style.Character); }
if("Alignment" in style) { textLayer.setTextAlignment(alignmentHash[style.Alignment]); }
if("Typeface" in style) { textLayer.setFontPostscriptName(style.Typeface); }
if("Color" in style) {
var color = MSColor.colorWithSVGString("#" + style.Color);
color.alpha = style.Opacity;
textLayer.setTextColor(color);
}
updateStyles(textLayer, style.Style);
}
function updateStyles(obj, name) {
log(obj.style().sharedObjectID());
var sharedStylesPredicate = NSPredicate.predicateWithFormat("objectID == %@", obj.style().sharedObjectID());
var sharedStyle = sharedStyles.objects().array().filteredArrayUsingPredicate(sharedStylesPredicate).firstObject();
if(sharedStyle) {
log('updating');
sharedStyles.synchroniseInstancesOfSharedObject_withInstance(sharedStyle, obj.style())
} else {
log('creating');
sharedStyles.addSharedStyleWithName_firstInstance(name, obj.style());
}
// doc.reloadInspector();
}
function removeAllStyles() {
var existingStyles = sharedStyles.objects();
while(existingStyles.count() > 0) {
style = existingStyles.objectAtIndex(0);
[existingStyles removeObject:style];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment