forked from Charimon/iosViews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_as_uiView.sketchplugin
executable file
·46 lines (38 loc) · 1.46 KB
/
export_as_uiView.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
// (ctrl alt cmd u)
//doc, selection, scriptPath
#import 'lib/common.js'
main()
function main() {
var loop = selection.objectEnumerator()
var str = ""
while(item = loop.nextObject()) {
str += processSelection(item, item.frame(), 0, 0)
}
paste(str)
}
function processSelection(layer, boundingFrame, offsetX, offsetY) {
if(!layer.isVisible()) return ""
var str = "func {{0}}(rect: CGRect){\nlet width = rect.width\nlet height = rect.height\n".format(safeName(layer))
forEach(layer.layers(), function(item){if(item.isVisible()) {
if(isShape(item)) {
str += "\n" + processShape(item, layer)
}
}})
str += "}\n"
return str
}
function processShape(layer, topLayer) {
if(hasSubShapes(layer)) { alert("warning: union, subtract, etc not supported") }
else {
var frame = layer.frame()
var x = round(frame.x()) + "*(width/{{0}})".format(topLayer.frame().width())
var y = round(frame.y()) + "*(height/{{0}})".format(topLayer.frame().height())
var w = round(frame.width()) + "*(width/{{0}})".format(topLayer.frame().width())
var h = round(frame.height()) + "*(height/{{0}})".format(topLayer.frame().height())
var viewName = "view_" + safeName(layer)
var str = "let {{4}} = UIView(frame: CGRect(x: {{0}}, y: {{1}}, width: {{2}}, height: {{3}}))\n".format(x,y,w,h,viewName)
str += "{{0}}.restorationIdentifier = \"{{1}}\"\n".format(viewName, layer.name())
str += "self.addSubview({{0}})\n".format(viewName)
return str
}
}