2

I know I can chagne the font size of an UIlabel dynamically by using auto-shrink. But there's no auto-shrink property for UIbuttons,

so how can I change the font size of my button dynamically according to the size of my button?

COde:

import UIKit

class ViewController: UITableViewController {

@IBAction func myButt(_ sender: UIButton) {}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    myButt.titleLabel?.adjustsFontSizeToFitWidth = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell

    return cell
}

}

Keon July
  • 21
  • 1
  • 4
  • 2
    Possible duplicate of [Adjust font size of text to fit in UIButton](https://stackoverflow.com/questions/32561435/adjust-font-size-of-text-to-fit-in-uibutton) – Vishnu gondlekar Sep 06 '17 at 05:38

3 Answers3

3

More specifically:

yourUIButtonName.titleLabel?.adjustsFontSizeToFitWidth = true

will fit the font size to the width of the button, and adjusting the content insets will allow you to pad the edges of the text.

However, if you are trying to change the font size in some way that is not directly proportional to the size of the button's label, you will probably have to get the CGRect and math it out as necessary.

  • It says: Value of type'(UIButton) ->()' has no member of 'titileLabel' – Keon July Sep 07 '17 at 02:59
  • May I see your code, specifically where you declare the button and where you apply the `adjustsFontSizeToFitWidth` property? It looks like you aren't calling a UIButton by itself. – afishershin Sep 07 '17 at 04:20
  • The way it's written right now, the first line creates a **function** `myButt()`. Consequently, `myButt.titleLabel?` won't exist–the function has no such member. To properly reference the button, you'll want to create an `@IBOutlet` for the button, which will get the button itself, not create a function. To do this, you can Ctrl + drag from the button itself or "New Referencing Outlet" in the Connections side panel for the button to inside the ViewController class. That should allow you to access the `.titleLabel` component. – afishershin Sep 07 '17 at 06:31
  • Thank you. It worked after I replaced the IBAction with an IBOutlet. – Keon July Sep 07 '17 at 07:55
2

You have to set minimumScaleFactor property too which specify the smallest multiplier for the current font size.

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        myButt.titleLabel?.adjustsFontSizeToFitWidth = true
        myButt.titleLabel?.minimumScaleFactor = 0.5 

    }
1

UIButton title is shown in a UILabel object. So you can set the property by accessing the titleLabel property of UIButton.

jegadeesh
  • 875
  • 9
  • 22