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

522

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

有没有办法将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];

对于Swift 5.1,您可以查看我的答案 https://dev59.com/gXE85IYBdhLWcg3wdDSz#65124739 - Ucdemir
15个回答

1676

设置contentHorizontalAlignment:

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

您可能还想调整内容的左缩进,否则文本将触及左边框:
// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

49
请记住,你也可以从Interface Builder设置这两个属性。 - Mihai Damian
1
所以这是我发出的第9000个数字,谢谢,非常有用的答案。 - Logic
9
我们太懒了,不想去查文档。如果没有 Stack Overflow(SO),我们该怎么办! - Kedar Paranjape
5
展示了 SO 的文档比苹果的文档更为详尽。 - GeneCode
是否可以在本地化时使用“Leading”或“Trailing”,而不是“Right”或“Left”? - Ricardo
2
UIControlContentHorizontalAlignmentLeadingUIControlContentHorizontalAlignmentTrailing是在iOS 11中添加的。 - Cal Stephens

125

如果不想在代码中进行调整,您还可以使用界面构建器。这里我将文本左对齐,并向右缩进一些:

UIButton in IB

不要忘记您也可以在按钮中对图像进行对齐:

enter image description here


这真的帮了我很多,简直不敢相信我之前没意识到那些控件是干什么用的。 - Ethan Parker
1
谢谢,这对我很有帮助。在您的示例图片中,没有箭头指示,我无法找到按钮标题的边缘插图。 - Ashok
在Xcode 8.2.1中,它已经移动到其他位置,请参见我的答案。 - dang
如何在Swift中以编程方式实现这一点?我想将图像对齐到右侧,标题对齐到左侧? - Visal Sambo

44

在Swift 3+中:

button.contentHorizontalAlignment = .left

.contentHorizontalAlignment的.leading和.left值有什么区别? - jamryu
@jamryu 在从右到左书写的语言(我想是阿拉伯语..?)中,.leading.right 具有相同的效果。 - Nat

20

Swift 4+

button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .top
button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

15
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

10

emailBtn.titleEdgeInsetscontentEdgeInsets更好,如果您不想改变按钮内整个内容的位置。


8

7
在Xcode 8.2.1中,在界面构建器中,它移动到:enter image description here

5

@DyingCactus的代码存在一个小错误。 以下是正确的解决方案,可以将UILabel添加到UIButton中,以更好地控制按钮的文本对齐:

NSString *myLabelText = @"Hello World";
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

// position in the parent view and set the size of the button
myButton.frame = CGRectMake(myX, myY, myWidth, myHeight); 

CGRect myButtonRect = myButton.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: myButtonRect];   
myLabel.text = myLabelText;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
myLabel.textAlignment = UITextAlignmentLeft;

[myButton addSubview:myLabel];
[myLabel release];

希望这能帮到您...
Al

3

针对Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

这可以帮助需要的任何人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接