1

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user180574
  • 5,681
  • 13
  • 53
  • 94

1 Answers1

0

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.

Community
  • 1
  • 1
Prathamesh Saraf
  • 709
  • 4
  • 14
  • It works, thank you! But it is not perfect because the animation is more like gradually appearing instead of from left to right as we would expect a strikethrough. would be better to improve. :) – user180574 May 30 '14 at 05:46
  • Your welcome. I know .. it kind of fades in.. I tried with others and this was the best one. – Prathamesh Saraf May 30 '14 at 06:15