UILabel动画调整大小副作用

发布于 2024-09-10 14:04:58 字数 292 浏览 7 评论 0原文

我遇到了需要对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-09-17 14:04:58

如果你想保持 origin.y 完整,那么你必须使用 CoreAnimation 来为其设置动画。
设置一次:

label.layer.anchorPoint = CGPointMake(0,0); //I believe 0,0 is the upper left or it was 0,1?

执行此操作后,您只需要相应地更改大小:

我会首先测量文本(使用 NSString 的方法)并查看标签是否需要调整大小。
如果需要调整 label.numberOfLineslabel.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:

label.layer.anchorPoint = CGPointMake(0,0); //I believe 0,0 is the upper left or it was 0,1?

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 and label.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文