Created
November 23, 2015 22:36
-
-
Save nolastan/81a95b414548fb073cc5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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