我做了以下內容:添加文字的CALayer:drawInRect不工作
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext (ctx);
CGRect r = CGRectMake(500, 300, 200, 100);
NSString *text = [[NSString alloc] initWithString:@"raaaaaaaa!"];
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
[color set];
[ text drawInRect: r withFont:[UIFont systemFontOfSize:50] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentLeft ];
[text release];
UIGraphicsPopContext();
}
}
}
上面的代碼無法正常工作。爲什麼?
,如果我做的不是跟隨,是工作:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIGraphicsPushContext (ctx);
CATextLayer *label = [[CATextLayer alloc] init];
[label setFont:@"Didot"];
[label setFontSize:50];
[label setFrame:CGRectMake(400, 300, 400, 100)];
[label setString:@"Helloooooooooooooooooo"];
[label setAlignmentMode:kCAAlignmentCenter];
[label setForegroundColor:[[UIColor blueColor] CGColor]];
[layer addSublayer:label];
[label release];
UIGraphicsPopContext();
}
}
的區別是什麼,爲什麼drawInRect什麼都不做?
謝謝
編輯 所以,我沒有使用正確的UIColor,所以我改變
UIColor *color = [ UIColor colorWithRed: (200.0f) green: (100.0) blue:(200.0f) alpha: 1.0f ];
此
UIColor *color = [UIColor colorWithRed:200/255.0 green:100/255.0 blue: 200/255.0 alpha: 1.0];
仍然沒有運氣,我不能使用drawInRect時看到任何文字
你與白色的白色層上繪製...(1添加此例)。 rgba值從0.0到1.0(不是255) – 2012-08-01 20:00:49
@DavidRönnqvist我以爲是的,但沒有運氣。使用drawIinRect時仍然沒有文字:( – peterphonic 2012-08-01 20:38:08