在Xcode界面构建器中创建动态调整大小的UILabel [英] Creating a dynamically sizing UILabel in the Xcode interface builder

查看:132
本文介绍了在Xcode界面构建器中创建动态调整大小的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个UILabel,它将以比普通AutoLayout更复杂的方式调整其大小.我已经在互联网上寻找了答案,但无济于事.

I want to be able to create a UILabel that will adjust it's size in a more complex way than normal AutoLayout. I've looked over the internet for an answer to this, but to no avail.

这就是我想要的.

假设您正在iPhone上查看它.我希望它与居中居中的任一边缘相距16点. (高度与这无关紧要)

Say you're viewing it on an iPhone. I want it to be 16 points away from either edge centered in the middle. (Height does not matter in any of this)

但是,当屏幕变宽时,我希望UILabel能够拉伸,以便与每个边缘(直到达到500宽度)相距16个点.一旦达到500宽度,我就不希望它变得更宽.这是两侧的16个点都增加的地方,但仍将标签保持在中心.

However, when the screen gets wider, I want the UILabel to stretch so that it's 16 points away form each edge UNTIL it reaches, say, 500 width. Once it reaches 500 width, I don't want it to get any wider. This is where the 16 points on either side increases, still keeping the label in the center.

现在,您正在iPad横向上查看它. UILabel恰好位于中央,宽500点.

Now you're viewing it on an iPad landscape. The UILabel is exactly 500 points wide and in the center.

如果可能的话,我希望能够使用AutoLayout而不是代码来完成此操作,但是如果必须使用代码,我可以解决.

If possible, I would like to be able to accomplish this using AutoLayout, and not code, but if code is a must, I can deal with that.

谢谢您的考虑.感谢所有帮助.

Thank you for your consideration. All help is appreciated.

推荐答案

您可以通过3个约束来做到这一点:

You can do this with 3 constraints:

  1. 将标签在视图中水平居中
  2. 将宽度限制设置为< = 500
  3. 将前导空间约束设置为16.将其优先级设置为<. 1000.

当视图较宽时(如在iPad上),标签将拉伸到其整个宽度500. Auto Layout (自动布局)将使标签居中,并通过将前导空格保持尽可能靠近16来尽力满足第3个约束.它选择打破此约束,因为优先级小于1000.

When the view is wide (like on an iPad), the label will stretch to its full width of 500. Auto Layout will keep the label centered, and it will try its best to satisfy the 3rd constraint by keeping the the leading space as close to 16 as possible. It chooses to break this constraint because the priority is less than 1000.

当视图较窄时(例如在iPhone上),标签的前导空格为16(由于标签居中,因此尾随空格为16).宽度将是剩下的任何宽度,因为它满足width <= 500约束.

When the view is narrow (like on an iPhone), the label will have a leading space of 16 (and trailing space of 16 because the label is centered). The width will be whatever is left, because that satisfies the width <= 500 constraint.

这篇关于在Xcode界面构建器中创建动态调整大小的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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