Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone CATextLayer doesn't show its text

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 to be displayed in the UIView, without any text. Just wonder what I missed to display the text.

Could anyone offer a hint/sample how to use CATextlayer?

  - (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;
    }
like image 427
user268743 Avatar asked Jun 18 '10 08:06

user268743


1 Answers

You should (counter-intuitively) call textLayer.display() or textLayer.displayIfNeeded() after the initialization is complete or whenever you want it to draw text.

like image 160
Aleksey Gureiev Avatar answered Oct 20 '22 00:10

Aleksey Gureiev