Frage

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?

War es hilfreich?

Lösung

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).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top