0

How to resize unbutton according to the text (fixed width but adjusted height)

 var radioButton = UIButton(frame: CGRectMake(renderWidth,renderHeight ,tableView.frame.width/4,20))
                    radioButton.setTitle("dsfasdsadsadsadsadasdasdsadasddsfdsfdsfdsfdsfdsfdsdsfdsfsdffdsfds", forState: .Normal)
                    radioButton.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
                    radioButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
                                         radioButton.titleLabel!.adjustsFontSizeToFitWidth = false
                    radioButton.backgroundColor = UIColor.greenColor()
henry
  • 83
  • 1
  • 13

1 Answers1

0
  1. Create UIView as wrapper with auto layout to views around.

  2. Put UILabel inside that wrapper. Add constraints that will stick to your label to edges of wrapper.

  3. Put UIButton inside your wrapper, then simple add the same constraints as you did for UILabel.

  4. Enjoy your autosized button along with text.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Himanshu
  • 2,832
  • 4
  • 23
  • 51