Skip to content

Instantly share code, notes, and snippets.

@vitsky91
Created February 18, 2022 11:12
Show Gist options
  • Save vitsky91/e8d4d142b2440f174306f15ec30ef1b0 to your computer and use it in GitHub Desktop.
Save vitsky91/e8d4d142b2440f174306f15ec30ef1b0 to your computer and use it in GitHub Desktop.
Convert String to UIBezierPath using Swift
import UIKit
import CoreText
import XCPlayground
let font = UIFont(name: "HelveticaNeue", size: 46)! // Choose font
var allPathes = [CGPath]()
var unichars = [UniChar]("i".utf16) //Insert text
var glyphs = [CGGlyph](repeating: 0, count: unichars.count)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
glyphs.forEach { glyph in
let cgpath = CTFontCreatePathForGlyph(font, glyph, nil)!
let path = UIBezierPath(cgPath: cgpath)
// ===========YOUR CODE
// let layer = CAShapeLayer()
// layer.path = path.cgPath
// layer.lineWidth = 0.5
// layer.strokeColor = UIColor.red.cgColor
// layer.fillColor = UIColor.gray.cgColor
// let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
// view.layer.addSublayer(layer)
// ===========YOUR CODE
print(path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment