我需要在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];

当前回答

Try

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

其他回答

对于Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

如果有人需要,这可以帮上忙。

Try

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

在Swift 3+中:

button.contentHorizontalAlignment = .left
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

如果您不想在代码中进行调整,也可以使用接口生成器。 这里我左对齐文本,也缩进一些:

别忘了你也可以在按钮中对齐图像。