在iOS中的两个对齐标签中调整文本大小

时间:2018-06-04 21:37:40

标签: ios swift autolayout storyboard

我有两个标签,并排,如下:

|--15--labelA--8--longerLabelB--15--|

我想让它们的所有字体大小在所有屏幕尺寸上都相等(一起增长和缩小),但是我得到一个错误,表示我需要设置其中一个标签'内容拥抱优先级更高,或内容压缩阻力优先级更高,这使得其中一个文本大小比另一个...

我尝试过使用stackview。没有运气。

我该如何实现?

2 个答案:

答案 0 :(得分:0)

如果你想让它们相同的大小控制 - 从任何一个拖动到另一个并选择Equal-Widths

或者

因为错误说明了你想要的任何一个,并将contentHuggingPriority / contenyCompresseionResistence设置为1000

答案 1 :(得分:0)

labelA和longLabelB都有其'内容大小,Apple称其为内在大小,它将对布局产生影响。请考虑以下两种情况(假代码):

let totalLength = 15 + contentSize(LabelA) + 8 + contentSize(LabelB) + 15 
if totalLength > containerSize.width {
   // which label should be shorter or compressed? LabelA or LabelB? 
   // answer: compress the one with lower compression resistance priority. 

   // compressed one would with content like: "xxxx....", truncated with "..." at last
} else { 
   // which label should be longer or hugging? LabelA or LabelB? 
   // answer: stretch the one with lower content hugging priority. 
}

因此,您可以将longLabelB的内容拥抱优先级设置为低于labelA的某个值。

如果您还希望LabelA的内容不像“xxxx ...”那样被压缩,请设置其' 压缩阻力优先级高于longLabelB

labelA应该是默认值,moreLabelB的设置快照如下:

enter image description here

注意:这两个优先级的实际值并不重要,关键是优先级,或者哪个更高或更低