Set UIButton title UILabel font size programmatically

前端 未结 18 2971
灰色年华
灰色年华 2020-12-07 07:16

I need to set the font size of the title UILabel of a UIButton programmatically.

相关标签:
18条回答
  • 2020-12-07 07:47

    I hope it will be help to you

    [_button.titleLabel setFont:[UIFont systemFontOfSize:15]];  
    

    good luck

    0 讨论(0)
  • 2020-12-07 07:47

    This way you can set the fontSize and can handle it in just one class.

    1. Created an extension of UIButton and added following code:

    - (void)awakeFromNib{
    
        [super awakeFromNib];
    
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont fontWithName:@"font" 
                                         size:self.titleLabel.font.pointSize]];
        [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    }
    

    2.1 Create UIButton inside Code

    Now if you create a UIButton inside your code, #import the extension of yourUIButton` and create the Button.

    2.2 Create Button in Interface Builder

    If you create the UIButton inside the Interface Builder, select the UIButton, go to the Identity Inspector and add the created extension as class for the UIButton.

    0 讨论(0)
  • 2020-12-07 07:47

    Check on custom font name whether checkbox on "Target membership" is added. This should help.

    0 讨论(0)
  • 2020-12-07 07:49

    Swift 3:

    myButton.titleLabel?.font = myButton.titleLabel?.font.withSize(40)
    
    0 讨论(0)
  • 2020-12-07 07:51

    you can also customise button font with bold, italic. this example with bold system font size.

    [LoginButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f*Ratio]];
    
    0 讨论(0)
  • 2020-12-07 07:52

    swift 4.x

    button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
    
    0 讨论(0)
提交回复
热议问题