I'm trying to understand why I can't add a UILabel's layer as a sublayer to another layer in a separate UIView object.
- (void)addNumber:(NSInteger)number toLayer:(CALayer *)layer {
UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))];
[numberLabel setFont:[UIFont boldSystemFontOfSize:12]];
[numberLabel setText:[NSString stringWithFormat:@"%d", number]];
/* if I change the BackgroundColor to an opaque color it renders as a solid black rect.
* No matter what color I choose
* Setting it as clear then it is transparent
*/
[numberLabel setBackgroundColor:[UIColor clearColor]];
[numberLabel setTextAlignment:NSTextAlignmentCenter];
[numberLabel setTextColor:[UIColor blackColor]];
CALayer *numberLayer = numberLabel.layer;
/* However creating a CATextLayer is successful
CALayer *numberLayer = [CATextLayer layer];
[numberLayer setFont:(__bridge CFTypeRef)([UIFont boldSystemFontOfSize:12])];
[numberLayer setBounds:CGRectMake(0, 0, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))];
[numberLayer setString:[NSString stringWithFormat:@"%d", number]];
[numberLayer setAlignmentMode:kCAAlignmentCenter];
[numberLayer setForegroundColor:[[UIColor whiteColor] CGColor]];
*/
[numberLayer setPosition:CGPointMake(CGRectGetMidX(layer.bounds),
CGRectGetMidY(layer.bounds) + CGRectGetMidY(numberLayer.bounds))];
[layer addSublayer:numberLayer];
}
However, if I was to create a CATextLayer instead, it works fine. (see commented out code)
My understanding is that every UIView subclass is backed by a root CALayer. Should I not be able to add that root CALayer to the sublayer hierarchy of another CALayer ?
Thank you for your help