iOS - 将两个标签放在超级视图的中心 [英] iOS - Placing two label together in centre of their super view

查看:150
本文介绍了iOS - 将两个标签放在超级视图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个标签,L1和L2的变量和非同一长度。我想在Xcode中使用约束放置这两个标签。如果可能的话,我不想以编程方式操纵约束。我们将不胜感激。

I have two label, L1 and L2 of variable and nonidentical length. I want to place these two label using constraints in Xcode. If possible i don't want to manipulate constraints programmatically. guidance will be appreciated.

正确的图像应该是预期的输出

Right image should be the expected output

要求,


  1. 两个标签是非宽度相同

  2. 当运行L1的前导空格并且L2的尾随空格将相等时

  3. L1与...之间的距离L2,水平空间固定,10 px

  4. UIStackView无法使用

  1. Two labels are non identical in width
  2. When running leading space of L1 and trailing space of L2 will be equal
  3. distance between L1 & L2, horizontal space is fixed, 10 px
  4. UIStackView cannot be used

我如何从Xcode中做到这一点?

How can I do it from Xcode?

推荐答案

你可以做的就是把 UILabel UIView 内,然后:

What you can do is put both the UILabel inside a UIView and then:


  • superView 的前导,顶部和底部添加到 HEL UILabel

  • superView 的尾部,顶部和底部添加到 HEAVENLY UILabel

  • 在两个 UILabel之间添加10个点的水平间距

  • Add a leading, top and bottom to the superView to your HEL UILabel.
  • Add a trailing, top and bottom to the superView to your HEAVENLY UILabel.
  • Add a horizontal spacing of 10 points between your two UILabel

现在在屏幕中间水平和垂直居中 UIView 。将水平居中约束优先级设置为250.使用 UIView 的前导和尾随空格添加到其 superView > = 5约束

Now center your UIView horizontally and vertically in the middle of the screen. Set the horizontally in center constraint priority to 250. Add a leading and trailing space of your UIView to its superView with a >= 5 constraint

编辑:此外,如果文字太大,则上述约束不是只需足以完美地工作(您必须将 numberOfLines 设置为0并将环绕模式设置为 wordWrap 以显示完整文本并转到下一行,当然)!由于 UILabel 将具有推断高度推断宽度,优先级为750且内容压缩阻力优先每个 UILabel 的默认值也是750,Xcode将会混淆该做什么,这是正确的!您可以做的是将属性检查器中的内容压缩属性设置为1000

Additionally if the text is too large then the above constraints aren't simply enough to work perfectly (You will have to set the numberOfLines to 0 and wrap mode to wordWrap to display the complete text and go to the next line, of course)! As the UILabels will have their inferred height and inferred width with a priority of 750 and the content compression resistance priority of each UILabel is 750 by default too, Xcode will be confused what to do and rightfully so! What you can do is set the content compression property in attributes inspector to 1000

如果您不希望修补压缩阻力(我以前发现它们很可怕),你还可以做的是在宽度上加一个> = 约束每个 UILabel 的高度。给出这个约束也可以显示完整的文本,无论它有多大,因为明确定义高度和宽度给它们优先级为1000!

If you don't wish to tinker with compression resistance (I used to find them scary), what you can also do is add a >= constraint to the width and height both of each UILabel. Giving this constraint will also work to show the complete text no matter how large it be, as explicitly defining height and width has given them a priority of 1000!

这篇关于iOS - 将两个标签放在超级视图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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