Autoshrink setting for UIButton in Storyboard
Asked Answered
T

5

43

There is a setting for UILabel in storyboard that allows setting auto-shrink configurations, as shown below:

enter image description here

But I am unable to find the same for UIButton's textlabel. I am aware that I can set this programmatically but curious to know if there's a way to enable this setting for UIButton in Storyboard.

Titian answered 8/3, 2016 at 6:1 Comment(0)
H
72

You can use User Defined Runtime Attributes to set this flag using the storyboard.

Set the following key path:

titleLabel.adjustsFontSizeToFitWidth to true

Adjust Font Size using Storyboard

Hithermost answered 22/3, 2016 at 9:52 Comment(0)
B
21

No, there is no option available in storyboard for set Button's textlabel auto-shrink ,

But you can set it programatically with adjustsFontSizeToFitWidth as you are aware with it.

yourbutton.titleLabel?.adjustsFontSizeToFitWidth = true;
Brendis answered 8/3, 2016 at 6:12 Comment(8)
you can set it via IBDesignable and IBInspectableCirilo
@Cirilo he just want to know that is it possible from storyboard. He want direct option like Autoshrink in UILabel.Brendis
yes but we can show him another approach via just couple line of code instead of telling him NO in boldCirilo
@Cirilo he already mention that he knows another approach with programatically.Brendis
@Cirilo this not only via storyboard , you also need to do set some properties programatically.Brendis
@Cirilo same here buddy. i also don't want to do. but can you say which option is better ? 2 line of code or full process with IBDesignable ?Brendis
via IBDesignable you can see the result directly without even running a code so it is betterCirilo
@Anbu.Karthik let's clear I downvoted your answer because you suggested deprecated methodCirilo
C
9

try this

btn.titleLabel.adjustsFontSizeToFitWidth = YES;
btn.titleLabel.minimumScaleFactor = 0.5; // set whatever factor you want to set 

If you want to set in storyboard try IBDesignable and IBInspectable

refer http://nshipster.com/ibinspectable-ibdesignable/

Cirilo answered 8/3, 2016 at 6:7 Comment(4)
so are you the downvoter for my answer, ok welcome bro, the question asked with out single code need to change , is this possible in UIButtonMarou
no hard feeling bro, it was deprecated so I did downvoteCirilo
IBDesignable or IBInspectable we are assign the custom proerty , in here we add our own property whatever we need corretMarou
with all due respect if we can set this property in the setter of our custom propertyCirilo
V
4

Swift 4 solution

class CustomButton : UIButton{
    @IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
        didSet {
            self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
        }
    }
}
Verbalize answered 9/10, 2017 at 12:5 Comment(0)
P
0
extension UIButton {
    @IBInspectable var adjustsTitleFontSizeToFitWidth: Bool = false {
        didSet {
            self.titleLabel?.adjustsFontSizeToFitWidth = adjustsTitleFontSizeToFitWidth
        }
    }
}
Projectionist answered 4/5, 2022 at 17:40 Comment(1)
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Sinistrad

© 2022 - 2024 — McMap. All rights reserved.