在 QuartzCore 中沿着路径绘制文本
假设我有一个点数组,它们形成一条线和一个文本。我怎样才能在
- (void)drawRect:(CGRect)rect
UIView 中沿着这条线绘制文本?
我能够毫无问题地绘制路径。是否有一个我忽略的标准方法或一个框架可以让我沿着该路径绘制文本?理想情况下,我想仅使用 QuartzCore/CoreGraphics 来完成此操作。
我尝试计算每个字符的宽度并旋转每个字符。这种方法可行,但我想知道是否有更优雅的方法。
Say I have an array of points that form a line and a text. How can I go about drawing the text along this line in
- (void)drawRect:(CGRect)rect
of a UIView?
I am able to draw the path without a problem. Is there a standard method that I overlooked or a framework that would allow me to draw the text along that path? Ideally I would like to do this using only QuartzCore/CoreGraphics.
I tried calculating the width of each character and rotating every character. This kind of works, but I was wondering if there is a more elegant method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您可以在 Mac OS X 中执行此操作,但在 iPhone 上最接近的是 CGContextShowGlyphsWithAdvances,而且它甚至不会旋转。
使用循环并使用如下所示的内容绘制每个字符应该不会太难。这是根据 Apple 文档改编的,未经测试,因此请注意:
编辑:这是 PositionAlongPath 函数的一个想法。同样,它们没有经过测试,但应该很接近。 originAlong... 如果超出路径,则返回 (-1, -1)。
I believe you can do this in Mac OS X, but the closest you'll come on the iPhone is CGContextShowGlyphsWithAdvances and this wont even rotate.
It shouldn't be too hard to use a loop and draw each character using something like the following. This is adapted from Apple's documentation and not tested, so beware:
Edit: Here's an idea of the PositionAlongPath functions. Again, they aren't tested, but should be close. originAlong... returns (-1, -1) if you run out of path.
这可能无关紧要,但您可以使用 SVG 沿路径发送文本 (http:// www.w3.org/TR/SVG/text.html#TextOnAPath),iPhoneOS 支持它。
This is probably irrelevant but you can text along a path with SVG (http://www.w3.org/TR/SVG/text.html#TextOnAPath), and the iPhoneOS supports it.
不幸的是,上面的例子没有按预期工作。
我现在终于找到了沿路径绘制文本的正确方法。
我们开始吧:
您不能将这段代码 1:1 当作它只是从我的应用程序中摘录的,但我会通过一些注释来澄清这一点。
Above example unfortunately didn't work as expected.
I have now finally found the correct way to paint a text along the path.
Here we go:
You cannot take this code 1:1 as its just excerpted from my application, but i will make things clear with some comments.