Set UIButton title UILabel font size programmatically

前端 未结 18 2969
灰色年华
灰色年华 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

    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.

提交回复
热议问题