如何使多行UILabel文本适合预定义的宽度而不包装中间单词 [英] How to make multi-line UILabel text fit within predefined width without wrapping mid-word

查看:59
本文介绍了如何使多行UILabel文本适合预定义的宽度而不包装中间单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UI Builder中精心设计了一个UILabel,并具有适当的高度和宽度限制.行数设置为4.自动换行设置为自动换行.文本为"CHECKED".字体大小非常大,因此仅适合"CHECKE",而"D"位于第二行.写"Checked"而不是"CHECKED"可使字体缩小(按预期方式),以便使整个单词合适.但是(文本是用户给定的,并且可以预期用户会写完全大写的单词)具有大写单词的标签不会像预期那样破坏它/缩小字体.

I have a UILabel carefully laid out in Interface Builder with proper height and width constraints. The number of lines is set to 4. The wrapping is set to word wrap. The text is "CHECKED". The font size is very large and thus it only fits "CHECKE" and the "D" is on the second line. Writing "Checked" instead of "CHECKED" lets the font shrink (as intended) so that the whole word fits. But (the text is user given and it can be expected that the user writes fully uppercase words) having uppercase words the label does not break it/shrink the font as expected.

关于我可能错过的事情,您有建议吗?大写单词(因此只将首字母大写)是可以的,但不是客户想要的.

Do you have a suggestion as to what I might have missed? Capitalising the words (thusly only having the first letter uppercase) does work, but is not what the client wants.

更新的问题

该问题似乎与大写或小写文本无关.我的问题可以通过回答以下问题来解决:

The problem seems to be unrelated to having uppercase or lowercase text. My problem could be solved by an answer to the following question:

如何(最好在Interface Builder的帮助下)缩小UILabel文本,以尝试在所有可用行中完全填充单词,而将文本中间的单词换行?

How to make (ideally with the help of only Interface Builder) the UILabel text shrink trying to fit full words within all available lines without wrapping the text mid-word?

  • 如果文本"CHECKED"对于标签来说太宽(可用行多于1行),则应缩小字体大小,而不要打断"D"并将单个字母包装到下一行.
  • 如果文本为"1 CHECKED 2",并且单个单词"CHECKED"对于标签来说已经太宽了(可使用多于1行),则应在所有单词之间之间进行切换字体大小,以使"CHECKED"仍然适合中间行.
  • If the text "CHECKED" is too wide for a label (with more than 1 line available) it should shrink the font size instead of breaking the "D" and wrapping the single letter to the next line.
  • If the text is "one CHECKED two" and the single word "CHECKED" is already too wide for a label (with more than 1 line available) it should break between all words and shrinking the font size so that "CHECKED" still fits the middle line.

避免:

one
CHECKE
D two

非常感谢!

推荐答案

我认为您只是遇到了自动收缩的限制.

I think you're just running into a limitation on Autoshrink.

在Interface Builder中:

In Interface Builder:

  • 使用Width: 230Height: 280
  • 添加新的UILabel
  • 将字体设置为System 44.0
  • 设置Line Break: Truncate Tail
  • Autoshrink: Minimum Font Scale设置为0.15
  • 将标签文本设置为test CHECKED lines
  • add a new UILabel with Width: 230 and Height: 280
  • set the Font to System 44.0
  • set Line Break: Truncate Tail
  • set Autoshrink: Minimum Font Scale at 0.15
  • set the text of the label to test CHECKED lines

现在,向左和向右拖动标签右边缘上的手柄...当它变得太窄时,单词CHECKED将中断到下一行.

Now, drag the handle on the right edge of the label left and right... when it gets too narrow, the word CHECKED will break onto the next line.

CHECKED更改为checked,然后执行相同的操作.您应该会看到相同的行为.

Change CHECKED to checked and do the same thing. You should see the same behavior.

现在,尝试上下拖动底部边缘.使用CHECKEDchecked,您应该看到字体大小自动缩小.

Now, try dragging the Bottom edge up and down. With either CHECKED or checked, you should see the Font Size auto shrink.

所以...要做您想做的事情,您可能必须跳过自动收缩,而要执行一些代码计算.

So... to do what you're trying to do, you might have to skip Autoshrink and instead do some code calculations.

进一步了解发生了什么...

further visual of what goes on...

  • 从上面的值开始,但是将标签的Height设置为170-给它一点垂直填充.
  • 现在,拖动左边缘使其变窄.
  • 当您到达单词CHECKED的末尾并继续前进时,您会看到字体缩小,直到字体变小到足以将其换行到第4行为止.
  • Start with above values, but set the Height of the label to 170 - gives it just a little vertical padding.
  • Now, drag the left edge to make it narrower.
  • When you reach the end of the word CHECKED, and keep going, you will see the font shrink until it gets small enough that there is space for it to wrap to a 4th line.

我认为您将需要一些代码来完全获得所需的内容.

I think you're going to need some code to get exactly what you need.

这篇关于如何使多行UILabel文本适合预定义的宽度而不包装中间单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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