-
-
Save victorchee/a00739da1c72b92b9a1d to your computer and use it in GitHub Desktop.
This file contains 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
func p_setupTextLayer(text: String) -> CAShapeLayer { | |
var letters = CGPathCreateMutable() | |
let font = CTFontCreateWithName("Helvetica-Bold", 72, nil) | |
let attrs = [kCTFontAttributeName: font] | |
var attrString = NSAttributedString(string: text, attributes: attrs) | |
let line = CTLineCreateWithAttributedString(attrString) | |
let runArray = CTLineGetGlyphRuns(line) | |
var run: CTRun = unsafeBitCast((CFArrayGetValueAtIndex(runArray, 0)), CTRun.self) | |
var runFont: CTFont = unsafeBitCast(CFDictionaryGetValue(CTRunGetAttributes(run), unsafeBitCast(kCTFontAttributeName, UnsafePointer.self)), CTFont.self) | |
for runGlyphIndex in 0 ..< CTRunGetGlyphCount(run){ | |
var thisGlyphRange = CFRangeMake(runGlyphIndex, 1) | |
var glyph = CGGlyph() | |
var position = CGPointZero | |
CTRunGetGlyphs(run, thisGlyphRange, &glyph) | |
CTRunGetPositions(run, thisGlyphRange, &position) | |
let letter = CTFontCreatePathForGlyph(runFont, glyph, nil) | |
var t = CGAffineTransformMakeTranslation(position.x, position.y) | |
CGPathAddPath(letters, &t, letter); | |
} | |
var path = UIBezierPath() | |
path.moveToPoint(CGPointZero) | |
path.appendPath(UIBezierPath(CGPath: letters)) | |
let layer = CAShapeLayer() | |
layer.frame = CGRectMake(10, 10, 180, 100) | |
layer.backgroundColor = nil | |
layer.geometryFlipped = true | |
layer.path = path.CGPath | |
layer.strokeColor = UIColor.whiteColor().CGColor | |
layer.fillColor = nil | |
layer.lineWidth = 3.0 | |
layer.lineJoin = kCALineJoinBevel | |
return layer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment