小编典典

如何将 UIButton 的标题设置为左对齐?

all

我需要在 a 的左侧显示一个电子邮件地址UIButton,但它被定位到中心。

有没有办法将对齐设置为 a 的左侧UIButton

这是我当前的代码:

UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
emailBtn.backgroundColor = [UIColor clearColor];
[emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
[emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailAction:) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:emailBtn];
[emailBtn release];

阅读 186

收藏
2022-03-11

共1个答案

小编典典

设置 contentHorizo​​ntalAlignment:

emailBtn.contentHorizontalAlignment = .left;

您可能还想调整左插图的内容,否则文本将触及左边框:

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

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);
2022-03-11