作者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
推 leondemon:謝謝Z大!CALayer的delegate是否不能是UIView? 04/13 23:03
→ leondemon:另外問一下為什麼要push/pop CGContextRef? 04/13 23:03
→ zonble:因為 NSString UIKit Additions 的繪圖,都是在 04/13 23:06
→ zonble:current context 裡頭做,所以我們要把 layer 所使用的 04/13 23:07
→ zonble:context 先變成 current context 來用,用完再設回去 04/13 23:07
→ zonble:我記得什麼 object 都可以當 CALayer 的 delegate 04/13 23:08
→ zonble:不過我個人習慣比較會選擇 subclass CALayer,然後實作 04/13 23:09
→ zonble:drawInContext:。因為 Layer 這種東西往往一用就很多 04/13 23:09
→ zonble:用 delegate 來管畫圖有點麻煩。 04/13 23:10
→ leondemon:嗯 因為我把delegate設為UIView App就會死當 =.= 04/13 23:31
→ leondemon:目前好像別人也是遇到這個情形... Orz 04/13 23:31