Swift 3-调整字体大小以适合宽度,多行 [英] Swift 3 - Adjust Font Size to Fit Width, Multiple Lines

查看:297
本文介绍了Swift 3-调整字体大小以适合宽度,多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel,它设置为42.0 pt字体,并且标签的宽度是根据标签本身以外的因素使用自动约束来设置的(也就是标签左右两侧的内容决定了标签的宽度) ).

I have a UILabel and it is set to 42.0 pt font, and the width of the label is set using autoconstraints based on factors other than the label itself (aka the things to the right and left of the label determine the label's width).

我想自动调整字体大小以适合标签的宽度,但是在可能的情况下也可以分成两行.与此类似:

I would like to auto-adjust the font size to fit the width of the label, however also break to two lines when it can. Similar to this:

我知道您可以调整字体大小以适合标签的宽度,但是仅当行数设置为1时.

I know you can adjust the font size to fit the width of the label, but only when the number of lines is set to 1.

我该怎么做?

推荐答案

这将起作用.

  • 设置标签的最小比例系数.如该图所示.
  • 设置行数= 2//或如果要更多行数则为零(0)
  • 将换行模式设置为2行的'.byTruncatingTail'
  • Set minimum scale factor for your label. as shown in this image.
  • Set number of lines = 2 // or zero (0) if you want more number of lines
  • Set line breaking mode to '.byTruncatingTail' for 2 lines

迅速3
将动态文本信息的行数设置为零,这对于更改文本很有用.

Swift 3
Set number of lines zero for dynamic text information, it will be useful for varying text.

var label = UILabel()
let stringValue = "A label\nwith\nmultiline text."
label.text = stringValue
label.numberOfLines = 2 // 0
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.5 // It is not required but nice to have a minimum scale factor to fit text into label frame

此外,请勿为标签设置高度限制超过2行.

Also, don't set height constraint for your label more than 2 lines.

这篇关于Swift 3-调整字体大小以适合宽度,多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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