【问题标题】:Animate transform a part of UILabel动画变换 UILabel 的一部分
【发布时间】:2014-12-18 07:37:41
【问题描述】:

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

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

【问题讨论】:

  • 我会为此使用 CATextLayer

标签: ios objective-c uilabel transform nsattributedstring


【解决方案1】:

我的建议是使用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
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多