Adjust UIButton font size to width
Try this:
button.titleLabel?.numberOfLines = 1
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.lineBreakMode = .byClipping //<-- MAGIC LINE
I'm not sure why this does the trick but it does :)
Adjust font size of text to fit in UIButton
self.mybutton.titleLabel.minimumScaleFactor = 0.5f;
self.mybutton.titleLabel.numberOfLines = 0; <-- Or to desired number of lines
self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES;
... did the trick, after layoutIfNeeded in viewDidLoad
As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame.
Update for Swift 3:
mybutton.titleLabel?.minimumScaleFactor = 0.5
mybutton.titleLabel?.numberOfLines = 0 <-- Or to desired number of lines
mybutton.titleLabel?.adjustsFontSizeToFitWidth = true
UIButton auto-adjust Button font size Swift
With auto layout you can set the space between buttons, and the max and min size. In code for all labels use:
self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;
With this all labels adjust the text will size.
For adjust button to titleLabel use auto layout constraint for titleLabel. For examples:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
This constraint define the height of titleLabel to 50% of self.view
height. Now you can adapte the constraint for that you need.
This work in you code?
How to adjust font size of text in a UIButton to fit the width programmatically in Swift?
Add this line for button object:
button.titleLabel?.adjustsFontSizeToFitWidth = true
How to adjust font size of text in a UIButton to fit the height programmatically in Swift?
Have a look at the baselineAdjustment
property of UILabel
:
It can be used with a UIButton as follows:
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
Adjust UIButton label font ,to screen size
You can try using CAT.titleLabel.minimumScaleFactor
to set the minimum font size. It takes in a multiplier value against the current font size.
Related Topics
How to Use a Custom Font with Dynamic Text Sizes in iOS7
How to Make View the Size of Another View in Swiftui
Creating a Navigationcontroller Programmatically (Swift)
How to Get an Iphone's Device Name
How to Set Uibutton Background Color Forstate: Uicontrolstate.Highlighted in Swift
How to Round Corners of Uiimage with Hexagon Mask
Expand Uilabel Inside Uitableview with "More" Button Like Instagram
iOS Protocol/Delegate Confusion
How to Disable Floating Headers in Uitableview with Uitableviewstyleplain
Native Facebook App Does Not Open with Facebook Login in iOS 9
How to Switch to Different Storyboard for iPhone 5
Swift iOS Check If Remote Push Notifications Are Enabled in iOS9 and iOS10
Record Audio and Save Permanently in iOS
Xcode - Mpnowplayinginfocenter Info Is Not Displayed on iOS 8