Skip to content

Instantly share code, notes, and snippets.

@webfrogs
Created July 27, 2014 23:21
Show Gist options
  • Save webfrogs/8a44f5393b83e089177b to your computer and use it in GitHub Desktop.
Save webfrogs/8a44f5393b83e089177b to your computer and use it in GitHub Desktop.
TextPath-Swift
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 = reinterpretCast(CFArrayGetValueAtIndex(runArray, 0))
var runFont: CTFont = reinterpretCast(CFDictionaryGetValue(CTRunGetAttributes(run), reinterpretCast(kCTFontAttributeName)))
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