获取跟踪iOS UIFont中的角色的路径 [英] Get path to trace out a character in an iOS UIFont

查看:121
本文介绍了获取跟踪iOS UIFont中的角色的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在我的iOS应用程序中使用了自定义字体Foo。我已经将它添加到我的项目,plist等等,我可以渲染 UILabel s等等。只需罚款。

Suppose I have a custom font "Foo" that I'm using in my iOS App. I've added it to my project, plist, etc. and I'm able to render UILabels and such with it just fine.

现在,如果我想找出一系列可以追踪该字体中字母P的点,我将如何获得该点序列?例如,假设我想使用 CGPath 来绘制字母P,就像绘图仪一样......

Now, if I want to find out a sequence of points that would "trace out" the letter 'P' in that font, how would I get that sequence of points? For example, suppose I wanted to use a CGPath to draw the letter 'P' slowly like a plotter would ...

我只需要一个数组中的 CGPoints 序列,如果绘制为路径则会绘制'P'。

I just need a sequence of CGPoints in an array that if drawn as a path would draw a 'P'.

我意识到对于像'T'这样的字母,这可能没有意义,因为笔必须被提升以越过'T'。所以也许我需要一个 CGPath 的序列...

I realize that for some letters like 'T', this may not make sense, since the pen would have to be lifted to cross the 'T'. So maybe I need a sequence of CGPaths ...

有关如何完成此任务的任何提示?

Any hints on how to accomplish this?

谢谢!

推荐答案

从字母P到序列分数涉及几个步骤。您需要使用核心文本。

Going from the letter "P" to a sequence of points involves several steps. You'll need to use Core Text.


  1. 创建 CTFont 。从iOS 7开始,您可以使用 UIFont ,其中需要 CTFont (它们是免费桥接) 。您还可以使用 CTFontCreateWithGraphicsFont CGFont 创建 CTFont c>函数,或使用 CTFontCreateWithName (或使用其他一些方法)的名称。

  1. Create a CTFont. Since iOS 7, you can use a UIFont where a CTFont is needed (they are "toll-free bridged"). You can also create a CTFont directly from a CGFont using the CTFontCreateWithGraphicsFont function, or by name using CTFontCreateWithName (or using a few other methods).

获取使用 CTFontGetGlyphsForCharacters 函数的字母的字形。对于字母P,应该只有一个字形。对于非英语脚本中的某些字符,您可能会获得多个(组合)字形。

Get the glyphs for the letter using the CTFontGetGlyphsForCharacters function. For the letter "P" there should be just one glyph. For some characters in non-English scripts you may get multiple (combining) glyphs.

使用 CTFontCreatePathForGlyph 函数获取字形的 CGPath

使用 CGPathApply 枚举路径的元素。

将路径的每个直线,四边形曲线和三次曲线元素转换为一个序列点。 Apple没有提供任何公共API来执行此操作。你需要自己做。对于直线元素,它很容易。对于曲线元素,如果您还不知道如何渲染Bézier曲线,则需要进行一些研究。例如,请参阅将贝塞尔曲线转换为多边形链?

Convert each line, quad curve, and cubic curve element of the path to a sequence of points. Apple doesn't provide any public API for doing this. You'll need to do it yourself. For straight line elements it's easy. For curve elements, you will need to do some research if you don't already know how to render a Bézier curve. For example, see convert bezier curve to polygonal chain?.

我们可以在Swift游乐场轻松实验:

We can experiment with this easily in a Swift playground:

import UIKit
import CoreText
import XCPlayground

let font = UIFont(name: "HelveticaNeue", size: 64)!

var unichars = [UniChar]("P".utf16)
var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
    let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)!
    let path = UIBezierPath(CGPath: cgpath)
    print(path)
    XCPlaygroundPage.currentPage.captureValue(path, withIdentifier: "glyph \(glyphs[0])")
}

结果:

<UIBezierPath: 0x7fbc89e0d370; <MoveTo {11.072000000000001, 23.808}>,
 <LineTo {11.072000000000001, 40.576000000000001}>,
 <LineTo {22.975999999999999, 40.576000000000001}>,
 <QuadCurveTo {30.560000000000002, 38.432000000000002} - {28.16, 40.576000000000001}>,
 <QuadCurveTo {32.960000000000001, 32.192} - {32.960000000000001, 36.288000000000004}>,
 <QuadCurveTo {30.560000000000002, 25.920000000000002} - {32.960000000000001, 28.096}>,
 <QuadCurveTo {22.975999999999999, 23.808} - {28.16, 23.744}>,
 <Close>,
 <MoveTo {4.992, 45.695999999999998}>,
 <LineTo {4.992, 0}>,
 <LineTo {11.072000000000001, 0}>,
 <LineTo {11.072000000000001, 18.687999999999999}>,
 <LineTo {25.024000000000001, 18.687999999999999}>,
 <QuadCurveTo {35.488, 22.208000000000002} - {31.936, 18.623999999999999}>,
 <QuadCurveTo {39.039999999999999, 32.192} - {39.039999999999999, 25.792000000000002}>,
 <QuadCurveTo {35.488, 42.143999999999998} - {39.039999999999999, 38.591999999999999}>,
 <QuadCurveTo {25.024000000000001, 45.695999999999998} - {31.936, 45.695999999999998}>,
 <Close>

这篇关于获取跟踪iOS UIFont中的角色的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆