Re: [問題] Draw a string in a specific CALayer

看板MacDev作者 (zonble)時間14年前 (2011/04/13 22:37), 編輯推噓1(1010)
留言11則, 2人參與, 最新討論串2/6 (看更多)
※ 引述《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

04/13 23:03, , 1F
謝謝Z大!CALayer的delegate是否不能是UIView?
04/13 23:03, 1F

04/13 23:03, , 2F
另外問一下為什麼要push/pop CGContextRef?
04/13 23:03, 2F

04/13 23:06, , 3F
因為 NSString UIKit Additions 的繪圖,都是在
04/13 23:06, 3F

04/13 23:07, , 4F
current context 裡頭做,所以我們要把 layer 所使用的
04/13 23:07, 4F

04/13 23:07, , 5F
context 先變成 current context 來用,用完再設回去
04/13 23:07, 5F

04/13 23:08, , 6F
我記得什麼 object 都可以當 CALayer 的 delegate
04/13 23:08, 6F

04/13 23:09, , 7F
不過我個人習慣比較會選擇 subclass CALayer,然後實作
04/13 23:09, 7F

04/13 23:09, , 8F
drawInContext:。因為 Layer 這種東西往往一用就很多
04/13 23:09, 8F

04/13 23:10, , 9F
用 delegate 來管畫圖有點麻煩。
04/13 23:10, 9F

04/13 23:31, , 10F
嗯 因為我把delegate設為UIView App就會死當 =.=
04/13 23:31, 10F

04/13 23:31, , 11F
目前好像別人也是遇到這個情形... Orz
04/13 23:31, 11F
文章代碼(AID): #1DfRKa-h (MacDev)
討論串 (同標題文章)
文章代碼(AID): #1DfRKa-h (MacDev)