UILabel字体大小适应标签宽度

huangapple go评论55阅读模式
英文:

UILabel font size fit label width

问题

        label.font = UIFont(name: "NanumBarunGothic-YetHangul", size: 10)
        label.numberOfLines = 0
        label.minimumScaleFactor = 0.5
        label.adjustsFontSizeToFitWidth = true

为什么返回大小为10?

我的预测是文本适合UILabel的宽度,并且具有最小大小为0.5的比例。

英文:
type swift
        label.font = UIFont(name: "NanumBarunGothic-YetHangul", size: 10)
        label.numberOfLines = 0
        label.minimumScaleFactor = 0.5
        label.adjustsFontSizeToFitWidth = true

Why is the size returned as 10?

My prediction is that the text fits within the width of the UILabel and has a minimum size of 0.5 scale.

答案1

得分: 0

adjustFontSize会在标签没有足够空间显示时生效(在adjustsFontSizeToFitWidth = false的情况下)。

标签将显示三个点,例如:some text...。如果您的标签具有numberOfLine = 0并且高度没有限制,则字体无需调整,因为内容仍然有足够的空间显示。

英文:

adjustFontSize will be active when the label doesn't have any space to display (in the case adjustsFontSizeToFitWidth = false).

The label will display three dots, ex: some text.... If your label has numberOfLine = 0 and does not have a limit on height, the font does not need to fit because the content still has enough space to display.

答案2

得分: 0

10号字体 作为起始点,并可以根据需要缩小,以确保所有文本适应标签宽度,同时保持最小字体缩放因子为0.5。

假设标签文本非常短,如"NanumBarun"。如果文本小到足以适应标签对象的宽度而无需缩小字体大小,则将使用代码中指定的10号字体大小。在这种情况下,minimumScaleFactoradjustsFontSizeToFitWidth 属性将不会产生任何影响,文本将以10号字体大小呈现,标签宽度的剩余空间将为空白。

英文:

The font size of 10 is used as a starting point, and it can be scaled down as needed to ensure that all the text fits within the label width, while maintaining a minimum font scale factor of 0.5.

Suppose the label text is very short, like "NanumBarun". If the text is small enough to fit within the width of the label object without scaling down the font size, then the font size of 10 will be used as specified in the code. In this case, the minimumScaleFactor and adjustsFontSizeToFitWidth properties will not have any effect, and the text will be rendered with the font size of 10 and the remaining space of the label width will be blank.

huangapple
  • 本文由 发表于 2023年3月20日 22:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定