0

Hi i am beginner in Ios and i am trying to inserted UIButton on my ViewController

Here i want to insert button "titlelabel" exactly at center of x-axis and center of y-axis and according to my below code button title label coming like below image please help me what should i do for set that "titlelabel" exactly center of x and y axis

my code:-

NextBtn_2 = [UIButton buttonWithType:UIButtonTypeCustom];
NextBtn_2.frame = CGRectMake(190, 330, 120, 30);
[NextBtn_2 setTitle:@"NEXT" forState:UIControlStateNormal];
NextBtn_2.titleLabel.font = [UIFont fontWithName:bitter_Bold size:14];
[NextBtn_2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[NextBtn_2 addTarget:self action:@selector(NextBtn_2_Clicked) forControlEvents:UIControlEventTouchUpInside];

NextBtn_2.titleLabel.textAlignment = NSTextAlignmentCenter;
[NextBtn_2 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[NextBtn_2 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

NextBtn_2.layer.cornerRadius = 3.0f;
NextBtn_2.layer.masksToBounds = YES;
NextBtn_2.backgroundColor = [self colorWithHexString:@"a53129"];

[self.view addSubview:NextBtn_2];

enter image description here

2 Answers2

1

Please try below Property of UIButton

NextBtn_2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
NextBtn_2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

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

Hope this is work for you.

Thanks :)

Rupal Patel
  • 570
  • 6
  • 13
0

Try to set titleEdgeInsets of your button title lable .

NextBtn_2.titleEdgeInsets = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) ;

hope it works for you .

Garry
  • 407
  • 2
  • 15