简体   繁体   中英

how can we set Button title labels exactly at center of both x-axis and y-axis

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];

在此处输入图片说明

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 :)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM