简体   繁体   English

如何为不同的屏幕设置自动调整大小的文本

[英]How to set auto size text for different screens

My task is to preserve the size and position of the elements inside my cell for different screen resolutions. 我的任务是为不同的屏幕分辨率保留单元格中元素的大小和位置。

I did: 我做了:

  1. Established constraints for the red and green blocks (UILabels) to the outer container. 为外部容器的红色和绿色块(UILabels)建立约束。
  2. Set the constraint between them equal to 0. It is more priority than the limitation of the red block to the bottom and green to the top. 将它们之间的约束设置为0。它的优先级高于红色块在底部和绿色在顶部的限制。
  3. Set Lines = 0 for these labels. 为这些标签设置行= 0。
  4. Set Autoshrink. 设置自动收缩。

As a result, the font size changes on different devices. 结果,字体大小在不同的设备上改变。 But there are still a few problems: 但是仍然存在一些问题:

  1. How can I remove too large paddings above and below both labels? 如何删除两个标签上方和下方的太大填充物?
  2. How to make them resize evenly? 如何使它们均匀地调整大小? Now one of the blocks has an advantage over the other, depending on what constraint to make a higher priority. 现在,其中一个块比另一个块具有优势,这取决于要赋予更高优先级的约束。 If you make them equal - it also does not work. 如果使它们相等-也将不起作用。

(I would like to do everything through Interface Builder) (我想通过Interface Builder做所有事情)

You can take advantage of equal height and equal width constraints. 您可以利用相等的高度和相等的宽度约束。

Follow the below steps to make the UILabel spacing same on iPhone 8 and iPhone 4s. 请按照以下步骤操作,以使iPhone 8和iPhone 4s上的UILabel间距相同。 This will help you to make it proportional. 这将帮助您使其成比例。

1) To achieve this simply select, your label(red), label (green) and the superview (which I think you're its a cell of UICollectionView ) 1)要实现此目的,只需选择您的label(红色),label(绿色)和superview(我认为您是UICollectionView的单元格)

在此处输入图片说明

2) We are interested here in keeping the height proportional. 2)我们对保持高度成比例感兴趣。 ie Red Label (80%) and Green Label (20%) 即红色标签(80%)和绿色标签(20%)

Currently, all heights are equal to superview height ie height of RedLabel and Greenlabel is equal to 100% of superview. 当前,所有高度都等于超级视图的高度,即RedLabel和Greenlabel的高度等于超级视图的100%。

But the goal is to make it 80% and 20% for red and green label respectively. 但目标是使红色和绿色标签分别达到80%和20%。

So select Red Label height constraint. 因此,选择红色标签高度约束。 Here you set the constraint which says's "height of the red label should be 80% of the superviews height". 在这里,您将设置约束条件,即“红色标签的高度应为超级视图高度的80%”。

Similarly for Green Label, set "height of the greeen label such that it's 20% for the superview's height". 同样,对于“绿色标签”,设置“绿色标签的高度,以使它占Superview高度的20%”。

Red Label 红标

在此处输入图片说明

Green Label 绿色标签

在此处输入图片说明

3) Now complete the x and y axis position constraint, which would be straight forward 3)现在完成x和y轴位置约束,这很简单

a) Red Label Leading = Leading edge of superview a)红色标签前导= Superview的前导边缘

b) Red Label Trailing = Trailing edge of superview b)红色标签尾随=超级尾随

c) Red Label Top = Top edge of superview c)红色标签顶部=超级视图的顶部边缘

d) Red Label Bottom = not required (as it has all the required constraint's to justify it's position ie height = 0.8 * superview and it is top aligned, ex: super view height is 100 , keep this view top aligned with height = 80 ) d)红色标签底部= 不需要 (因为它具有所有必要的约束条件来证明其位置即高度= 0.8 *超级视图并且顶部对齐,例如:超级视图高度为100,使该视图顶部与height = 80对齐)

e) Green Label Leading = Red Label Leading (you have already set this in point "a" no need to set the constraint again for Green label) e)绿色标签领先=红色标签领先(您已经在点“ a”中设置了此功能,无需再次为绿色标签设置约束)

f) Green Label Trailing = Red Label Leading (you have already set this in point "b" no need to set the constraint again for Green label) f)绿色标签结尾=红色标签开头(您已经在点“ b”中设置了此标签,无需再次为绿色标签设置约束)

g) Green label bottom = bottom of superview g)绿色标签底部=超级视图底部

h) Green label top = not required (as it has all the required constraint's to justify it's position ie height = 0.2 * superview and it is bottom aligned, ex: super view height is 100 , keep this view bottom aligned with height = 20 ) h)绿色标签top = 不需要 (因为它具有所有必要的约束条件以证明其位置正确,即height = 0.2 * superview并与底部对齐,例如:super view的高度为100,使此视图底部与height = 20对齐)

This is the final constraint list and the storyboard preview for iPhone 8 and 4s. 这是最终的约束列表以及iPhone 8和4s的情节提要预览。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM