Adjust font size of text to fit in UIButton

前端 未结 9 1012
予麋鹿
予麋鹿 2020-12-08 02:12

I want to make sure the button text fits into a UIButton, while the UIButton has a fixed size.

Of course I can access the titleLabel of the UIButton. In a label I w

9条回答
  •  孤城傲影
    2020-12-08 02:57

    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
    

提交回复
热议问题