UIFont: how to animate strikethrough and match font style?

白昼怎懂夜的黑 提交于 2019-12-11 09:43:24

问题


(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!