I have square buttons whose size vary whether it's displayed on an iPad or iPhone. I want the font of the buttons title to adjust to the size of the buttons, ie. so that they don't look too small on the bigger iPad screen, or too big on a smaller iPhone screen.
I came up with the following solution :
// Buttons is an outlet collection
for button in Buttons {
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleEdgeInsets = UIEdgeInsetsMake(button.frame.height/3, button.frame.width/3, button.frame.height/3, button.frame.width/3)
button.titleLabel!.numberOfLines = 1
button.titleLabel!.minimumScaleFactor = 0.1
button.clipsToBounds = true
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.alignCenters
print(button.titleLabel!.font.pointSize)
}
This provides an adjustment of the size of the font based on the width of the title. So buttons with a shorter title will have a bigger font than buttons with a longer title.
I want the same font size for all buttons, so I would like to access the adjusted size of one of them (let's say the smallest) to set it to all buttons. How could I do that ?
Alternatively I was thinking to adjust the font to the button height and not width but couldn't find a solution that worked.