简体   繁体   English

调整UIButton标签字体,以适应屏幕尺寸

[英]Adjust UIButton label font ,to screen size

I have buttons that created in code, and their size is in % from the screen size, so they change their width and height proportionally to the screen . 我有一些用代码创建的按钮,它们的大小是屏幕大小的%,因此它们的宽度和高度与屏幕成比例地变化。

The only thing that remains in the same size is the font size . 唯一保持不变的大小是字体大小。

I know there is a way to set minimum font size with : 我知道有一种方法可以设置最小字体大小:

  CAT.titleLabel.font=[UIFont fontWithName:@"Gill Sans" size:12];
CAT.titleLabel.numberOfLines=1;
CAT.titleLabel.minimumFontSize=2; //not good

But, the last row here ,minimum size ,is deprecated ,and so Xcode will not allowed to use it. 但是,不建议使用此处的最后一行(最小大小),因此Xcode将不允许使用它。

So , i was also try to set it to fit to width with : 因此,我也尝试将其设置为适合宽度:

CAT.titleLabel.adjustsFontSizeToFitWidth=YES;

Which results in different font sizes for buttons with the same width and height , so i can see 10 buttons in the same frame ,and different text length but with completely different font size .I guess its width is the same as the text width and not the button frame.width . 这导致宽度和高度相同的按钮的字体大小不同 ,因此我可以在同一帧中看到10个按钮,并且文本长度不同,但字体大小却完全不同。我猜它的宽度与文本宽度相同,但不是按钮frame.width。

Is there other solution to fit the frame ? 还有其他适合框架的解决方案吗?

You can try using CAT.titleLabel.minimumScaleFactor to set the minimum font size. 您可以尝试使用CAT.titleLabel.minimumScaleFactor设置最小字体大小。 It takes in a multiplier value against the current font size. 它采用乘以当前字体大小的值。

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

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