OGeek|极客世界-中国程序员成长平台

标题: ios - NSAttributedText 字体大小根据 UILabel 宽度变化 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:59
标题: ios - NSAttributedText 字体大小根据 UILabel 宽度变化

我已经用这种方式创建了一个NSAttributedString

attribute3=[
          NSForegroundColorAttributeName: innercircleColor,
          NSFontAttributeName:UIFont.init(name: "Bariol", size: 18.0)!]

attriType=NSAttributedString.init(string: type, attributes: attribute3)
lblType.setAttributedText(attriType)

但根据我的 UILabel 大小,在 iPhone 5s 中,此属性文本位于 UILabel 之外。如何使属性文本字体大小适合 UILabel 宽度



Best Answer-推荐答案


您想要的是 UILabel 上的 adjustsFontSizeToFitWidth 属性。它会自动调整 Font 的大小以适合框架。

您还可以使用 numberOfLines 属性设置用于设置文本的行数,还可以使用 minimumFontSize 属性设置字体应缩放到的最小大小。

https://developer.apple.com/documentation/uikit/uilabel/1620546-adjustsfontsizetofitwidth

关于ios - NSAttributedText 字体大小根据 UILabel 宽度变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786007/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4