有没有办法动画更改UILabel的textAlignment? [英] Is there a way to animate changing a UILabel's textAlignment?

查看:133
本文介绍了有没有办法动画更改UILabel的textAlignment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 7,我正在尝试移动一个位于视图左侧中心的标签。目前我试图通过改变我的UILabel的对齐方式来做到这一点,并且我正在尝试在此过程中为其设置动画。我目前正在调用以下内容:

I am using iOS 7 and I am trying to move a label that is centered off to the left of my view. Currently I am trying to do this by changing how my UILabel is aligned, and I am trying to animate it in the process. I am currently calling the following:

[UIView animateWithDuration:0.5
                 animations:^{
                      self.monthLabel.textAlignment = NSTextAlignmentLeft;
                 } completion:nil];

但这只是将标签跳转到新的对齐方式。有没有办法动画这个,或者更好的方法来调整标签以实现这一点?

But this just jumps the label to the new alignment. Is there a way to animate this, or a better way to adjust the label to make this happen?

推荐答案

设置UILabel框架大小以准确包含文本并将UILabel置于视图中心。

Set the UILabel frame size to exactly contain the text and center the UILabel in your view.

self.monthLabel.text = @"February";
[self.monthLabel sizeToFit];
self.monthLabel.center = parentView.center; // You may need to adjust the y position

然后设置不应影响布局的对齐方式没有额外的空间。

Then set the alignment which should not affect the layout since there will be no extra space.

self.monthLabel.textAlignment = NSTextAlignmentLeft;

接下来,设置UILabel帧大小的动画,使其滑过您想要的位置。

Next, animate the UILabel frame size so it slides over where you want it.

[UIView animateWithDuration:0.5
             animations:^{
                  CGRect frame = self.monthLabel.frame;
                  frame.origin.x = 10;
                  self.monthLabel.frame = frame;
             } completion:nil];

这篇关于有没有办法动画更改UILabel的textAlignment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆