Ios 为UILabel的一部分设置变换动画

Ios 为UILabel的一部分设置变换动画,ios,objective-c,uilabel,transform,nsattributedstring,Ios,Objective C,Uilabel,Transform,Nsattributedstring,假设我有一个这样的文本:“今天下午3点见我”,我想用动画将“今天”部分从原始大小缩放到x1.2,然后再返回(只是一种视觉效果,以吸引用户注意文本的该部分) 我如何做到这一点?简单的解决方案是使用3个分开的标签,并为中间标签设置CGAffineTransformMakeScale动画,但这是一个可怕的解决方案,因为当文本发生变化时(例如,当我们将其本地化为其他语言时),很难做到这一点.我的建议是使用NSAttributedString并使用CADisplayLink刷新标签。大概是这样的: CAD

假设我有一个这样的文本:“今天下午3点见我”,我想用动画将“今天”部分从原始大小缩放到x1.2,然后再返回(只是一种视觉效果,以吸引用户注意文本的该部分)


我如何做到这一点?简单的解决方案是使用3个分开的标签,并为中间标签设置CGAffineTransformMakeScale动画,但这是一个可怕的解决方案,因为当文本发生变化时(例如,当我们将其本地化为其他语言时),很难做到这一点.

我的建议是使用
NSAttributedString
并使用
CADisplayLink
刷新标签。大概是这样的:

CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(animatePartOfText)];
    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

-(void)animatePartOfText
{
    //calculate the size
    CGFloat size = 0 ;//......
    self.textLable.attributedText = [self makeTextWithPartSize:size];
}

-(NSAttributedString*)makeTextWithPartSize:(CGFloat)size
{
    NSAttributedString *firstPart = [[NSAttributedString alloc] initWithString:@"Meet me " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    NSAttributedString *animatedPart = [[NSAttributedString alloc] initWithString:@"today" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size]}];
    NSAttributedString *lastPart = [[NSAttributedString alloc] initWithString:@" at 3 p.m" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:firstPart];
    [text appendAttributedString:animatedPart];
    [text appendAttributedString:lastPart];
    return text;
}

我不确定性能,但它应该可以工作。

您的方法返回NSString,它应该返回NSMutableAttributedString。比如:-(NSMutableAttributedString*)makeTextWithPartSize:(CGFloat)Size我会使用CATextLayer来实现这一点