我做了以下事情:
- (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 时我看不到任何文字