Skip to content

Commit 1de8fdc

Browse files
committed
fixed plugin
1 parent 350c6fe commit 1de8fdc

14 files changed

+610
-396
lines changed

README.md

+50-54
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@ iosViews
33

44
Sketch plugin to generate ios view code
55

6-
###export_as_cgContext.sketchplugin
7-
exports your views as CGContext as swift code
6+
###pathExporter.sketchplugin (finally fixed)
7+
exports your views as UIBezierPath as swift code
88
so you'll have something like
99

10-
CGContextMoveToPoint(...)
11-
CGContextAddCurveToPoint(...)
10+
let path = UIBezierPath()
11+
path.moveToPoint(...)
1212
...
1313

1414
####Why?
1515
It's very annoying to draw in ios in code. But it's annoying to draw in sketch and export it as a picture because you can no longer use the CGRect dimensions from ios. You're stuck with whatever dimensions you picked in sketch.
1616

17-
This will generate a bunch of swift functions, with 1 main function which takes in a CGRect so you can put it in drawRect. All your shapes will be relative to how you depict them in sketch and the CGRect. Also you can edit the code and add your own transformations or animations, instead of having to create a ton of assets and load them dynamically.
17+
This will generate a bunch of swift functions, with 1 main function which takes in a CGRect so you can put it in layoutSubviews. All your shapes will be relative to how you depict them in sketch and the CGRect. Also you can edit the code and add your own transformations or animations, instead of having to create a ton of assets and load them dynamically.
1818

1919
For instance, if in sketch you offset your creation 20px from the left, and your arboard is 200px in width. Then at runtime of the ios application your creation will be offset 20*(width/200) where width is the width of the ios CGRect at runtime. This of course works with y offset, width, and height.
2020

2121
When you run the plugin it puts the generated code into your artboard, so if you have a very complex shape (or groups of shapes) pasting the code might be slow or even crash sketch. If it is complex and hasn't crashed, it might take minutes (spinning beachball) but it will work.
2222

23-
Once I figure out how to make sketch save it to a file and NOT crash sketch, I'll do that and it should work much better.
2423

2524
####How does it work?
2625
1. create an artboard (let's say iphone sized)
2726
1. create some shape you want that you'd have to do in code otherwise
2827
1. select the artboard (make sure it's only the artboard, this is very important)
2928
1. SAVE, because sketch might might crash if you have weirdly intercepting paths in your shape (flatten them, and try to simplify them)
30-
1. run the export_as_cgContext plugin
29+
1. run the pathExporter.sketchplugin
3130
- anything hidden will be ignored
32-
- you will get code where everything is relative to the selection (artboard in this case)
33-
- for each group in the selection, the plugin will generate a swift function named after the group (make sure these are unique)
31+
- you will get code where everything is relative to the artboard
3432
- NOTE: for complex shapes the export might be slow (take minutes)
3533

3634
####Unfortunatelly this doesn't work with:
@@ -45,8 +43,7 @@ Once I figure out how to make sketch save it to a file and NOT crash sketch, I'l
4543
1. shadows, inner shadows, blurs, reflections are ignored
4644
1. character spacing, line height, paragraph spacing are ignored
4745
1. justified alignment is ignored
48-
1. text decoration and list types are ignored
49-
1. text weight is ignored
46+
1. text layers are ignored
5047
1. typeface might be iffy (I've only tested Helvetica Neue, and that's what ios defaults to if it can't find your typeface)
5148
1. even/odd setting on fill is ignored (it uses whatever ios uses, I don't know if it's even/odd or non zero by default)
5249
1. all border settings (ends, joints, start arrow, end arrow, dash, gap) are ignored
@@ -55,64 +52,63 @@ Once I figure out how to make sketch save it to a file and NOT crash sketch, I'l
5552
- fix a lot of the things that don't work
5653
- settle on a good shortcut
5754
- add this to sketch toolbox
58-
- make it export to a file, rather than cliboard (which is currently makes the pasting teeeerribly slow)
55+
- make it export to a file, rather than cliboard (which is currently makes the pasting teeeerribly slow if you're pasting into xcode)
5956

6057
####Example
6158
#####Sketch File
62-
![moo png](https://github.com/Charimon/iosViews/blob/master/moo.png?raw=true "moo.png")
59+
![moo png](https://github.com/Charimon/iosViews/blob/master/moo_sketch.png?raw=true "moo_sketch.png")
6360

6461
#####Generated Code
6562
```swift
66-
func mooWithSign(rect:CGRect){
67-
let ctx = UIGraphicsGetCurrentContext()
68-
let width = rect.width
69-
let height = rect.height
70-
var pstyleRightAlign = NSMutableParagraphStyle()
71-
pstyleRightAlign.alignment = NSTextAlignment.Right
72-
var pstyleLeftAlign = NSMutableParagraphStyle()
73-
pstyleLeftAlign.alignment = NSTextAlignment.Left
74-
var pstyleCenterAlign = NSMutableParagraphStyle()
75-
pstyleCenterAlign.alignment = NSTextAlignment.Center
76-
77-
outline(rect)
78-
79-
body(rect)
80-
81-
//sign
82-
CGContextSaveGState(ctx)
83-
CGContextMoveToPoint(ctx, 611.29*(width/1024), 267.75570408534463*(height/1024))
84-
CGContextAddCurveToPoint(ctx, 605.87*(width/1024), 266.7*(height/1024), 602.33*(width/1024), 261.45*(height/1024), 603.38*(width/1024), 256.0305932082729*(height/1024))
85-
CGContextAddCurveToPoint(ctx, 603.38*(width/1024), 256.03*(height/1024), 618.65*(width/1024), 177.5*(height/1024), 618.65*(width/1024), 177.5005438434377*(height/1024))
86-
CGContextAddCurveToPoint(ctx, 619.7*(width/1024), 172.08*(height/1024), 624.95*(width/1024), 168.54*(height/1024), 630.37*(width/1024), 169.59298574057823*(height/1024))
87-
CGContextAddCurveToPoint(ctx, 630.37*(width/1024), 169.59*(height/1024), 807.06*(width/1024), 203.94*(height/1024), 807.06*(width/1024), 203.93723204167492*(height/1024))
88-
CGContextAddCurveToPoint(ctx, 812.48*(width/1024), 204.99*(height/1024), 816.03*(width/1024), 210.24*(height/1024), 814.97*(width/1024), 215.66234291874667*(height/1024))
89-
CGContextAddCurveToPoint(ctx, 814.97*(width/1024), 215.66*(height/1024), 799.71*(width/1024), 294.19*(height/1024), 799.71*(width/1024), 294.19239228358185*(height/1024))
90-
CGContextAddCurveToPoint(ctx, 798.65*(width/1024), 299.62*(height/1024), 793.4*(width/1024), 303.15*(height/1024), 787.98*(width/1024), 302.09995038644126*(height/1024))
91-
CGContextAddCurveToPoint(ctx, 787.98*(width/1024), 302.1*(height/1024), 701.6*(width/1024), 285.31*(height/1024), 701.6*(width/1024), 285.30944522664595*(height/1024))
92-
CGContextAddCurveToPoint(ctx, 701.6*(width/1024), 285.31*(height/1024), 663.53*(width/1024), 481.13*(height/1024), 663.53*(width/1024), 481.13443929250855*(height/1024))
93-
CGContextAddCurveToPoint(ctx, 663.37*(width/1024), 481.96*(height/1024), 662.59*(width/1024), 482.49*(height/1024), 661.77*(width/1024), 482.33029560656144*(height/1024))
94-
CGContextAddCurveToPoint(ctx, 660.96*(width/1024), 482.17*(height/1024), 660.43*(width/1024), 481.38*(height/1024), 660.59*(width/1024), 480.56201230637885*(height/1024))
95-
CGContextAddCurveToPoint(ctx, 660.59*(width/1024), 480.56*(height/1024), 698.65*(width/1024), 284.74*(height/1024), 698.65*(width/1024), 284.73701824051636*(height/1024))
96-
CGContextAddCurveToPoint(ctx, 698.65*(width/1024), 284.74*(height/1024), 611.29*(width/1024), 267.76*(height/1024), 611.29*(width/1024), 267.76*(height/1024))
97-
CGContextSetStrokeColorWithColor(ctx, UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 1).CGColor)
98-
CGContextSetLineWidth(ctx, 8*min(width/1024, height/1024))
99-
CGContextSetFillColorWithColor(ctx, UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor)
100-
CGContextDrawPath(ctx, kCGPathFillStroke)
101-
CGContextRestoreGState(ctx)
102-
}
63+
lazy var outline: CAShapeLayer = {
64+
let l = CAShapeLayer()
65+
l.lineWidth = 28
66+
l.strokeColor = UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor
67+
l.fillColor = UIColor(hue: 0, saturation: 0, brightness: 1, alpha: 1).CGColor
68+
self.layer.addSublayer(l)
69+
return l
70+
}()
10371
...
72+
func moo(bounds: CGRect) {
73+
outline.frame = bounds
74+
outline.path = outline_path(outline.frame)
75+
body.frame = bounds
76+
body.path = body_path(body.frame)
77+
spot2.frame = bounds
78+
spot2.path = spot2_path(spot2.frame)
79+
spot.frame = bounds
80+
spot.path = spot_path(spot.frame)
81+
armLeft.frame = bounds
82+
armLeft.path = armLeft_path(armLeft.frame)
83+
armRight.frame = bounds
84+
armRight.path = armRight_path(armRight.frame)
85+
head.frame = bounds
86+
head.path = head_path(head.frame)
87+
snout.frame = bounds
88+
snout.path = snout_path(snout.frame)
89+
nostrilLeft.frame = bounds
90+
nostrilLeft.path = nostrilLeft_path(nostrilLeft.frame)
91+
nostrilRight.frame = bounds
92+
nostrilRight.path = nostrilRight_path(nostrilRight.frame)
93+
eyeLeft.frame = bounds
94+
eyeLeft.path = eyeLeft_path(eyeLeft.frame)
95+
eyeBallLeft.frame = bounds
96+
eyeBallLeft.path = eyeBallLeft_path(eyeBallLeft.frame)
97+
eyeRight.frame = bounds
98+
eyeRight.path = eyeRight_path(eyeRight.frame)
99+
eyeBallRight.frame = bounds
100+
eyeBallRight.path = eyeBallRight_path(eyeBallRight.frame)
101+
}
104102
```
105103
[full swift file](../moo.swift)
106104

107105
#####ios
108106
after adding the following to some UIView
109107
```swift
110-
override func drawRect(rect: CGRect) {
108+
override func layoutSubviews(rect: CGRect) {
111109
moo(rect)
112110
}
113111
```
114112
you should see
115-
![moo_ios png](https://github.com/Charimon/iosViews/blob/master/moo_ios.png?raw=true "moo_ios.png")
113+
![moo_ios png](https://github.com/Charimon/iosViews/blob/master/moo_iphone.png?raw=true "moo_iphone.png")
116114

117-
###export_as_uiView
118-
currently does nothing, I want this to export things as tagged UIView's where the tag is the name of the group

moo.png

-152 KB
Binary file not shown.

0 commit comments

Comments
 (0)