I want to write some words to one layer. but when I create the layer,I can't find the corresponding API to attach the words to the layer.
layer2 = [CALayer layer];
[layer2 setBackgroundColor:[[UIColor grayColor] CGColor]];
layer2.bounds = CGRectMake(0, 0, 6开发者_开发技巧0,40);
layer2.position = CGPointMake(25,25);//
layer2.contentsRect = layer2.bounds;
layer2.contents=@"Hello World~~"; //It's nothing in the showing layer .
[self.layer addSublayer:layer2];
contents
property works only with CGImageRef
. If you wish to use text on it, use CATextLayer
.
Example Usage
CATextLayer * textLayer = [CATextLayer layer];
textLayer.backgroundColor = [[UIColor darkGrayColor] CGColor];
textLayer.foregroundColor = [[UIColor whiteColor] CGColor];
textLayer.bounds = CGRectMake(0, 0, 60, 40);
textLayer.position = CGPointMake(25, 25);
textLayer.string = @"Hello World";
textLayer.font = CGFontCreateWithFontName(CFSTR("Helvetica"));
textLayer.fontSize = 15.0;
[self.window.layer addSublayer:textLayer];
Of course, I am leaking memory with CGFontCreateWithFontName
. You can fix that by assigning it to a variable and releasing it later.
精彩评论