CGPathRef from string

前端 未结 2 1428
野的像风
野的像风 2021-02-04 20:52

Please how can we get path of particular arabic of french letter ? I\'ve just found out that CTFontCreatePathForGlyph will give CGPathRef like, but its will be the outline of te

2条回答
  •  别跟我提以往
    2021-02-04 21:38

    I needed this in Swift but it was painful to work out. I hope this is useful to someone else! I've written it as extensions to String and NSAttributedString to be more versatile.

    You might see the letters as upside-down depending on how you're drawing your path. You can fix this by adding a vertical flip transform to the call to CTFontCreatePathForGlyph() (a vertical flip is just a CGAffineTransform with a scaleY of -1).

    public extension String {
        func path(withFont font: UIFont) -> CGPath {
            let attributedString = NSAttributedString(string: self, attributes: [.font: font])
            let path = attributedString.path()
            return path
        }
    }
    
    public extension NSAttributedString {
        func path() -> CGPath {
            let path = CGMutablePath()
    
            // Use CoreText to lay the string out as a line
            let line = CTLineCreateWithAttributedString(self as CFAttributedString)
    
            // Iterate the runs on the line
            let runArray = CTLineGetGlyphRuns(line)
            let numRuns = CFArrayGetCount(runArray)
            for runIndex in 0..

提交回复
热议问题