2

I'm trying to create a char by char animation in UILabel with attributed text

//set attributed string
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Yaba daba doo"];
[attributedString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, [attributedString length])];

//set animation
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
    ^{
        [self animateShowText:[attributedString string] characterDelay:0.1 withCurrentLabel:self.myLabel];
        [self.myLabel setAttributedText:attributedString];
    });

I was hoping that the last message would set the text with the attribute, and could not find a solution. Here, I was trying to animate the string regular style, and the have the label display the attributed string, and it did not work. How do you keep attribute and animate text?

1 Answer 1

2

You should update views just on the main thread. Use performSelectorOnMainThread:withObject: to do so inside the block:

[self.myLabel performSelector: @selector(setAttributedText:) withObject: attributedString];

If animate show text calls method that update views you should do the same, or the alternative is to use dispatch_sync (at this point I would say don't even use GCD).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.