UILabel动画调整大小副作用
我遇到了需要对 UILabel 框架或其封闭视图框架的高度变化进行动画处理的问题。标签是多行的。
问题是,给定的大文本最初不适合标签(假设需要 3 行),然后动画标签的高度增加,立即将 3 行更改为 4 行,然后动画帧增加。
相反的效果是,当文本完全适合标签(4 行)时,然后我将高度减小动画,4 行立即变为 3 行,然后我才看到动画帧大小减小。
这当然对眼睛不好。
我期望的是保持标签的 origin.y 完整,然后随着框架的增加,更多的文本从底部显露出来。省略号可能会立即转换为缺失的单词,这不是问题。
I came across issue where I need to animate height change of UILabel frame or its enclosing view's frame. Label is multiline.
The issue is that given with large text which does not fit initially into label(say it takes 3 lines), then animating the label's height to increase, immediately changing 3 line to 4 and then animating the frame increase.
Opposite effect is when the text fully fit into label(4 lines), then I animate height decrease, 4 lines are instantly becoming 3 and only then I see animating frame size decrease.
This is of course not good for an eye.
What I expect is something like keep the label's origin.y intact and then as frame is increasing the more text is revealing from the bottom. The ellipsis may convert to missing word instantly, that is not a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想保持 origin.y 完整,那么你必须使用 CoreAnimation 来为其设置动画。
设置一次:
执行此操作后,您只需要相应地更改大小:
我会首先测量文本(使用 NSString 的方法)并查看标签是否需要调整大小。
如果需要调整
label.numberOfLines
和label.layer.bounds = CGRectMake(label.layer.bounds.origin.x,label.layer.bounds.origin.y,
label.layer.size.width, newHeight);
这应该有效
我希望这会有所帮助。
If you want to keep origin.y intact then you have to animate it with CoreAnimation stuff.
Set once:
after doing this you just need to change the size accordinly:
I would measure the text first (With NSString's methods)and see if the label needs to be resized.
In case it needs to then adjust
label.numberOfLines
andlabel.layer.bounds = CGRectMake(label.layer.bounds.origin.x,label.layer.bounds.origin.y,
label.layer.size.width, newHeight);
This should work
I hope this helps.