user1179321
user1179321

Reputation: 801

Dynamically change text of CATextLayer

I've been banging my head off a wall trying to figure this problem out. I've added a CATextLayer to a CALayer and after a couple of operations happen i want to change the text of the CATextLayer to something different. Is there a simple way to do that? Below i have some code below which doesn't seem to work.

self.searchingForTextLayer = [CATextLayer layer];
    self.searchingForTextLayer.bounds = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
    self.searchingForTextLayer.string = @"Searching for..";
    self.searchingForTextLayer.fontSize = 12;
    //TextLayer.font = [UIFont boldSystemFontOfSize:18].fontName;
    self.searchingForTextLayer.backgroundColor = [UIColor clearColor].CGColor;
    self.searchingForTextLayer.position = CGPointMake(80.0, 40.0f);
    self.searchingForTextLayer.wrapped = NO;
    [self.captureVideoPreviewLayer addSublayer:self.searchingForTextLayer];

and later i call this in a method

self.searchingForTextLayer.string = @"Search complete";

but calling this doesnt change the text on screen.

Any help would be great!

Upvotes: 3

Views: 1869

Answers (1)

pstoppani
pstoppani

Reputation: 2571

Make sure you are changing the string property on the main thread and disable animations. This worked for me (Swift):

 dispatch_async(dispatch_get_main_queue(), { () -> Void in
                CATransaction.begin()
                CATransaction.setDisableActions(true)
                self.textLayer?.string = text
                CATransaction.commit()
            })

Upvotes: 3

Related Questions