将文本添加到CALayer [英] Add text to CALayer

查看:569
本文介绍了将文本添加到CALayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 UILabel 添加到 CALayer ,而无需进行子类化并在 drawInContext:?

Is it possible to add a UILabel to a CALayer without subclassing and drawing it in drawInContext:?

谢谢!

推荐答案

我认为你不能将一个UIView子类添加到CALayer对象中。但是,如果要在CALayer对象上绘制文本,可以使用NSString UIKit中提供的绘图函数来完成添加,如下所示。虽然我的代码是在委托的drawLayer:inContext方法中完成的,但同样可以在子类'drawInContext:方法中使用。您是否想要利用任何特定的UILabel功能?


I don't think you can add a UIView subclass to a CALayer object. However if you want to draw text on a CALayer object, it can be done using the drawing functions provided in NSString UIKit additions as shown below. While my code is done in the delegate's drawLayer:inContext method, the same can be used in subclass' drawInContext: method. Is there any specific UILabel functionality that you want to leverage?

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]);

  UIGraphicsPushContext(ctx);
  /*[word drawInRect:layer.bounds 
          withFont:[UIFont systemFontOfSize:32] 
     lineBreakMode:UILineBreakModeWordWrap 
         alignment:UITextAlignmentCenter];*/

  [word drawAtPoint:CGPointMake(30.0f, 30.0f) 
           forWidth:200.0f 
           withFont:[UIFont boldSystemFontOfSize:32] 
      lineBreakMode:UILineBreakModeClip];

  UIGraphicsPopContext();
}

这篇关于将文本添加到CALayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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