问题
(a) How to animate strikethrough? I tried the following but not working.
-(void)strikeThrough
{
NSNumber *strikeSize = [NSNumber numberWithInt:1];
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:_label.text attributes:strikeThroughAttribute];
_label.attributedText = nil;
[UIView animateWithDuration:2.0
animations:^{ _label.attributedText = strikeThroughText; }
completion:^(BOOL finished){ }];
}
(b) Also, the style of strikethrough does not match with the font. For example, I use chalk font, but the strike line does not look like chalk. How to deal with it?
Thanks a lot for the help.
回答1:
One way of having a have an animation for it is to use CATransition
NSNumber *strikeSize = [NSNumber numberWithInt:1];
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:self.mylabel.text attributes:strikeThroughAttribute];
self.mylabel.attributedText = nil;
CATransition *transition = [CATransition new];
transition.delegate = self;
transition.type = kCATransitionFromLeft;
transition.duration = 2.0f;
self.mylabel.attributedText = strikeThroughText;
[self.mylabel.layer addAnimation:transition forKey:@"transition"];
and for the completion block
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//Check for CATransition here
}
For your second point check here . The link briefly gives you other ways to handle the way you want the strike to look like, you cannot change the font of it.
Hope this helps.
来源:https://stackoverflow.com/questions/23925664/uifont-how-to-animate-strikethrough-and-match-font-style