views:

220

answers:

3

Here is my new bee issue: I was simply trying to add a CATextlayer in an UIView layer. However, according to the following code, I only get the CATextlayer's background color be displayed in the UIView, without any text. Just wonder what did I missed to display the text.

Could anyone ofter a hint/sample how to use CATextlayer? Thanks.

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization

    CATextLayer *TextLayer = [CATextLayer layer];
    TextLayer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
    TextLayer.string = @"Test";
    TextLayer.font = [UIFont boldSystemFontOfSize:18].fontName;
    TextLayer.backgroundColor = [UIColor blackColor].CGColor;
    TextLayer.wrapped = NO;
    
    
    //TextLayer.backgroundColor = [UIColor blueColor];
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
    self.view.backgroundColor = [UIColor blueColor];
    [self.view.layer addSublayer:TextLayer];
    [self.view.layer layoutSublayers];
    

    } return self; }

A: 

According to the docs, the default text color of a CATextLayer is white. White on white is hard to see.

Ole Begemann
Thanks for your post!After I changed color to black, still no text shown. Any further recommendation?
A: 

try this one:

...

self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];

[self.view setWantsLayer:YES];

self.view.backgroundColor = [UIColor blueColor];

...

Andrew Gubanov
A: 

Change your code to this:

CATextLayer *TextLayer = [CATextLayer layer];
TextLayer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
TextLayer.string = @"Test";
TextLayer.font = [UIFont boldSystemFontOfSize:18].fontName;
TextLayer.backgroundColor = [UIColor blackColor].CGColor;
TextLayer.position = CGPointMake(80.0, 80.0f);
TextLayer.wrapped = NO;
[self.view.layer addSublayer:TextLayer];

You should also be doing this in the view controller's -viewDidLoad. That way you know your view is loaded and valid and can now have layers added to it.

Matt Long