简体   繁体   English

我们如何才能将Button标题标签准确地设置在x轴和y轴的中心

[英]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 嗨,我是Ios的初学者,我正在尝试在ViewController上插入UIButton

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 在这里,我想将按钮“ titlelabel”恰好插入x轴中心和y轴中心,并根据我的下面的代码按钮标题标签如下图所示,请帮助我设置“ titlelabel”正中心位置x和y轴

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 请尝试下面的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 . 尝试设置按钮标题标签的titleEdgeInsets

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

hope it works for you . 希望对你有效 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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