简体   繁体   English

NSAttributedText字体大小根据UILabel宽度而变化

[英]NSAttributedText font size changes according to the UILabel width

I have created a NSAttributedString in this way 我以这种方式创建了NSAttributedString

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

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

But according to my UILabel size, in iPhone 5s this attributed text is coming outside the UILabel. 但是根据我的UILabel大小,在iPhone 5s中,此属性文本超出了UILabel。 How can I make the attributed text font size fit to the UILabel width 如何使属性文本字体大小适合UILabel宽度

What you want is the adjustsFontSizeToFitWidth property on the UILabel . 您需要的是UILabel上的adjustsFontSizeToFitWidth属性。 It will auto size the Font to fit the frame. 它将自动调整Font大小以适合框架。

You can also set the number of lines to use to set the text using the numberOfLines property and also the minimum size the font should scale to using the minimumFontSize property. 您还可以使用numberOfLines属性设置用于设置文本的行数,还可以使用minimumFontSize属性设置字体应缩放到的最小尺寸。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM