Adjust font size of text to fit in UIButton

前端 未结 9 996
予麋鹿
予麋鹿 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 03:12

    In Xcode->Open storyboard->Go to attributes inspector->Control->Alignment->Choose second in both horizontal and vertical.

    or

    YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    0 讨论(0)
  • 2020-12-08 03:13

    Try to call the following method.

    button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
    
    0 讨论(0)
  • 2020-12-08 03:14

    You can also set a User Defined Runtime Attribute in Interface Builder for the button where

    titleLabel.adjustsFontSizeToFitWidth = true
    
    0 讨论(0)
提交回复
热议问题