3

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1179321
  • 797
  • 1
  • 10
  • 24
  • It worked fine for me. Added two extra lines but you would have done that too. self.captureVideoPreviewLayer = [CATextLayer layer]; [self.captureVideoPreviewLayer addSublayer:self.searchingForTextLayer]; [[self.view layer] addSublayer:self.captureVideoPreviewLayer]; – ratul Jul 02 '13 at 20:26
  • yeah that part works when when i try to change the text after i have already added it as a sublayer it wont change – user1179321 Jul 02 '13 at 20:46
  • All s fine for me. Before changing the text, make sure whether 'self.searchingForTextLayer' is pointing to the right layer. – ratul Jul 04 '13 at 06:18

1 Answers1

3

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()
            })
pstoppani
  • 2,531
  • 1
  • 20
  • 20