简体   繁体   中英

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.

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 .

Is there other solution to fit the frame ?

You can try using CAT.titleLabel.minimumScaleFactor to set the minimum font size. It takes in a multiplier value against the current font size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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