Quartz 2d / Core Graphics:绘制文本的正确方法是什么?
我已经研究这个问题有一段时间了,似乎在quartz 2d中有很多方法可以解决这个问题:
1)使用核心图形方法绘制文本......
CGContextSelectFont
CGContextSetRGBFillColor
CGContextShowTextAtPoint
等等,这是非常低水平的。
2)使用 NSString drawAtPoint
(到目前为止我喜欢的方法)
NSString* text = @"Hello";
[text drawAtPoint:point withFont:font];
3)使用 UILabel
我在某处读过这个,但不太确定这是否可能。但我认为在drawRect中实例化一个UILabel会非常昂贵,因为drawRect可能被调用无数次。 ??
目前我对 2.(使用 NSString drawAtPoint)做得很好,但我想得到一些意见。再次,我尝试在drawRect(我对视图进行子类化)中创建文本,因为我也在随文本一起绘制形状,正确的方法是什么?
谢谢
I've been at this for awhile, it seems that there's many ways to go about this in quartz 2d:
1) Draw text using core graphics methods...
CGContextSelectFont
CGContextSetRGBFillColor
CGContextShowTextAtPoint
and on and on, which is horribly low level.
2) using NSString drawAtPoint
(so far the method I like)
NSString* text = @"Hello";
[text drawAtPoint:point withFont:font];
3) using UILabel
I've read this somewhere but not too sure if this is possible. but I'm thinking that instantiating a UILabel within drawRect would be pretty costly as drawRect probably gets called a zillion times. ??
I'm doing ok with 2. (using NSString drawAtPoint) for the moment but I wanted to get some opinions. Again, I'm trying to create text within drawRect (I subclassed a view) because I'm also drawing shapes along with text, what is the right way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#1 不支持国际字符。因此,如果您使用它来绘制用户输入的文本,请不要使用它。
因此,请使用#2,除非您绝对确定文本仅包含标准 ASCII 字符。
#1 doesn't support international characters. So, don't use it if you use it to draw texts inputted by a user.
So, please use #2 unless you're absolutely sure that the text only contains standard ASCII characters.
#2 是我通常的做法,除非我需要使用 CG/CT 进行更精确的控制
#2 is how I generally do it unless I need the more precise control of using CG/CT
虽然我同意 Yuji 的观点,即您不应该使用#1,但我想补充一点,如果可能的话,您应该使用 UILabel 来代替自定义绘图,因为如果您可以完全避免实现 -drawRect:,任何地方,你都应该。
While I agree with Yuji that you should not use #1, I would add that if at all possible you should use a UILabel instead of custom drawing, because if you can avoid implementing -drawRect: at all, anywhere, you should.