iOS sets the display position of UIButton text and the font size and color

  • 2020-12-19 21:13:00
  • OfStack

preface

It is well known that UIButton button is the most commonly used control in IOS development. As a basic IOS learning tutorial knowledge, beginners need to understand its basic definition and common Settings in order to skillfully use it in development.

iOS sets the font size of UIButton


btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
// Sets the size of self on the button 
//[btn setFont: [UIFont systemFontSize: 14.0]]; // This can be used to set the font size, but may be in the future SDK Remove the change method from the version 
// You should use 
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
// Finally, adds the button to the specified view superView
[superView addSubview: btn];

2. iOS sets the text display location of UIButton


tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

It's initialized this way button , the default color of the text is white, so if the background is also white, the text will not be visible.


btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;// Set the text position, now set to left, default is center 
[btn setTitle:@ " title " forState:UIControlStateNormal];//  To add text 

Sometimes we want to UIButton the title Left-aligned, we set


btn.textLabel.textAlignment = UITextAlignmentLeft

It doesn't work. We need to set it up


btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

But again, the problem is that the text is going to be close to the border, and we can set it


btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

Make the text border 10 pixels apart.

3. iOS sets the font color of UIButton

Set up the UIButton Color Settings for the font on UIButton The color of the font on, instead of:


[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];

But with:


[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

conclusion

That's the end of this article, I hope to be helpful to you iOS developers, if you have any questions you can leave a message to exchange.


Related articles: