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

当前回答

下面解释一下如何做到这一点以及为什么它是这样的: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

其他回答

使用emailBtn。titleEdgeInsets比contenttedgeinsets更好,如果你不想改变按钮内的整个内容位置。

Try

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

下面解释一下如何做到这一点以及为什么它是这样的: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

在xcode 8.2.1的接口构建器中,它移动到:

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