作者zonble (zonble)
看板MacDev
标题Re: [问题] Draw a string in a specific CALayer
时间Wed Apr 13 22:37:22 2011
※ 引述《leondemon (狗狗)》之铭言:
: 想请教一个问题
: 我有一个UIView的subclass 打算将其(CALayer*)layer内多叠几层其他的CALayer
: 其中一个sublayer是专门要画text用的
: 因为CATextLayer的string似乎是没有anti-aliasing
: 而用NSString的NSString UIKit Additions Category的drawInRect:WithFont:
: 似乎不是画在指定的Layer上?
写段 sample code:
...
CALayer *aLayer = [CALayer layer];
aLayer.frame = CGRectMake(10.0, 10.0, 200.0, 100.0);
aLayer.delegate = self;
[aLayer setNeedsDisplay];
[self.view.layer addSublayer:aLayer];
...
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext(ctx);
[[UIColor blueColor] set];
[@"Hello World!" drawInRect:layer.bounds withFont:[UIFont boldSystemFontOfSize:16.0]];
UIGraphicsPopContext();
}
--
zonble.net
cocoa.zonble.net
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.42.192.132
1F:推 leondemon:谢谢Z大!CALayer的delegate是否不能是UIView? 04/13 23:03
2F:→ leondemon:另外问一下为什麽要push/pop CGContextRef? 04/13 23:03
3F:→ zonble:因为 NSString UIKit Additions 的绘图,都是在 04/13 23:06
4F:→ zonble:current context 里头做,所以我们要把 layer 所使用的 04/13 23:07
5F:→ zonble:context 先变成 current context 来用,用完再设回去 04/13 23:07
6F:→ zonble:我记得什麽 object 都可以当 CALayer 的 delegate 04/13 23:08
7F:→ zonble:不过我个人习惯比较会选择 subclass CALayer,然後实作 04/13 23:09
8F:→ zonble:drawInContext:。因为 Layer 这种东西往往一用就很多 04/13 23:09
9F:→ zonble:用 delegate 来管画图有点麻烦。 04/13 23:10
10F:→ leondemon:嗯 因为我把delegate设为UIView App就会死当 =.= 04/13 23:31
11F:→ leondemon:目前好像别人也是遇到这个情形... Orz 04/13 23:31