'How set UIButton size by it's title size?

I have UIButton, which title is dynamic changes. Button size should changes with title size and will be equal title size.

How to do this programmatically in Swift?



Solution 1:[1]

To have your button use its intrinsic content size and automatically resize based upon its text, use Auto Layout to position the button. Only set constraints to position the button and iOS will use the size of the text to determine the width and height of the button.

For example:

let button = UIButton()

// tell it to NOT use the frame
button.translatesAutoresizingMaskIntoConstraints = false

button.setTitle("Hello", for: .normal)
view.addSubview(button)

button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

This also works if you create the button in the Storyboard. Again, only give constraints to place the button and it will resize to accommodate the text.

Solution 2:[2]

You can get UIButton's width and Height dynamically with its title.

With the help of, NSString's Size property we can achieve this.

let buttonNAme = [" hi ", "welcome", "Login", "Forgot Password ??", "New to here. Sign up??"]
var yPos = CGFloat()

override func viewWillAppear(_ animated: Bool) {

    yPos = 40

    for i in 0..<buttonNAme.count
    {
        self.view.addSubview(addingCustomButton(buttonTitle: buttonNAme[i], buttonFontSize: 15, buttonCount: i))
    }
}

func addingCustomButton(buttonTitle : String, buttonFontSize: CGFloat, buttonCount : Int) -> UIButton
{
    let ownButton = UIButton()

    ownButton.setTitle(buttonTitle, for: UIControlState.normal)


    ownButton.titleLabel?.font = UIFont.systemFont(ofSize: buttonFontSize)

    let buttonTitleSize = (buttonTitle as NSString).size(attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: buttonFontSize + 1)])

    ownButton.frame.size.height = buttonTitleSize.height * 2
    ownButton.frame.size.width = buttonTitleSize.width
    ownButton.frame.origin.x = 30

    yPos = yPos + (ownButton.frame.size.height) + 10

    ownButton.frame.origin.y = yPos 

    ownButton.tintColor = UIColor.white
    ownButton.backgroundColor = .brown

    ownButton.tag = buttonCount

    ownButton.setTitleColor(UIColor.darkGray, for: UIControlState.highlighted)
    ownButton.addTarget(self, action: #selector(ownButtonAction), for: UIControlEvents.touchUpInside)

    return ownButton
}

func ownButtonAction(sender: UIButton)
{
    print("\n\n Title  \(sender.titleLabel?.text)  TagNum    \(sender.tag)")
}

Output

enter image description here

Solution 3:[3]

Follow below steps(its not a proper solution but you can solve your problem by doing like this )

  1. Create a UILabel (because UILabel adjust its height and width depends on the text)
  2. UIlabel number of line to 1
  3. Create a UIButton over UILabel
  4. Set button title to ""
  5. Set button's constraint : Align button's top and leading to UILabel and equals width and height

Hope this will works for you :)

Solution 4:[4]

Just Constraint it's origin, and the size will fit it's button title

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Shekhar Gaur
Solution 4 0x0