-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportAsUILabel.sketchplugin
149 lines (120 loc) · 3.91 KB
/
exportAsUILabel.sketchplugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// (ctrl alt cmd c)
/*
ExportUILabel from TextLayer
Created By: Toth Csaba
2014
*/
main()
//Default size is what you working on the sketch file. For example if you are working with 2x sizes make it 2, if 1x make it to 1
var defaultSize;
function main()
{
defaultSize = 2;
// alertForDefaultSize();
var allItems= @"\n\n";
var loop = selection.objectEnumerator()
while(item = loop.nextObject())
{
if ([item isMemberOfClass:[MSTextLayer class]])
{
allItems = addNewParametersToStr(allItems, handleTextbox(item));
}
else
{
log(@"Please select a textBox object");
}
}
// var page = [doc currentPage]
// var layers = [page children]
// [page deselectAllLayers]
// var loop = [layers objectEnumerator]
// while (layer = [loop nextObject])
// {
// if([layer isMemberOfClass:[MSTextLayer class]])
// {
// allItems = addNewParametersToStr(allItems, handleTextbox(layer));
// [layer select:true byExpandingSelection:true]
// }
// }
// log(page);
log(allItems);
saveToPasteBoard(allItems);
}
function alertForDefaultSize()
{
// var userinput = [doc askForUserInput:"What sizes you using?" initialValue:@"2x"];
// defaultSize = [userinput stringByReplacingOccurrencesOfString:@"x" withString:@""];
var availableSizes = [@"1x",@"2x",@"3x"];
var accessory = [[NSComboBox alloc] initWithFrame:NSMakeRect(0,0,200,25)]
[accessory addItemsWithObjectValues:availableSizes]]
[accessory selectItemAtIndex:1]
var alert = [[NSAlert alloc] init]
[alert setMessageText:@"What sizes are you using?"]
[alert addButtonWithTitle:'OK']
[alert addButtonWithTitle:'Cancel']
[alert setAccessoryView:accessory]
var responseCode = [alert runModal]
var sel = [accessory indexOfSelectedItem]
var selectionString = availableSizes[sel];
defaultSize = [selectionString stringByReplacingOccurrencesOfString:@"x" withString:@""];
}
function handleTextbox(textbox)
{
if (textbox && textbox.class()!= MSTextLayer)
{
return "";
};
var name = safeName(textbox);
var str = "UILabel *"+name +"= [UILabel new];";
str = addNewParametersToStr(str, "["+name+" setFont:[UIFont fontWithName:@\""+textbox.fontPostscriptName()+"\" size:"+textbox.fontSize()/defaultSize+"]];");
str = addNewParametersToStr(str, "["+name+" setTextColor:"+UIColorFromColor(textbox.textColor())+"];");
str = addNewParametersToStr(str, "["+name+" setTextAlignment:"+NSTextAligmentFromTextAligment(textbox.textAlignment()))+"];");
str = addNewParametersToStr(str, "["+name+" setFrame:CGRectIntegral(CGRectMake("+textbox.frame().x()/defaultSize+","+textbox.frame().y()/defaultSize+", "+textbox.frame().width()/defaultSize+", "+textbox.frame().height()/defaultSize+"))];");
log(str);
return str;
}
function NSTextAligmentFromTextAligment(textAlignment)
{
var aligment = textAlignment;
switch(textAlignment)
{
case 0:
aligment = @"NSTextAlignmentLeft";
break;
case 1:
aligment = @"NSTextAlignmentRight";
break;
case 2:
aligment = @"NSTextAlignmentCenter";
break;
case 3:
aligment = @"NSTextAlignmentJustified";
break;
}
return aligment;
}
function UIColorFromColor(color)
{
return "[UIColor colorWithRed:"+color.red().toFixed(3)+" green:"+color.green().toFixed(3)+" blue:"+color.blue().toFixed(3)+" alpha:"+color.alpha().toFixed(3)+"]"
}
function addNewParametersToStr(originalStr, additions)
{
return originalStr += "\n"+additions;
}
function saveToPasteBoard(str)
{
var pasteBoard = NSPasteboard.generalPasteboard();
pasteBoard.declareTypes_owner( [ NSPasteboardTypeString ], null );
pasteBoard.setString_forType_(str, NSStringPboardType);
}
function safeName(layer)
{
var name = layer.name()
return name.stringByReplacingOccurrencesOfString_withString_(" ", "_")
}
function round(value, decimals)
{
if(decimals == null) decimals = 2
var power = Math.pow(10, decimals)
return Math.round(value * power)/power
}