如何为左对齐设置的UIButton的标题? [英] How to set the title of UIButton as left alignment?

查看:260
本文介绍了如何为左对齐设置的UIButton的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从的UIButton 的左边显示的电子邮件地址,但它被定位在中央。

有没有什么办法来对齐设置的左侧的UIButton

这是我目前的code:

 的UIButton * emailBtn = [UIButton的页头] initWithFrame:方法CGRectMake(5,30,250,身高+ 15)]。
emailBtn.backgroundColor = [的UIColor clearColor]
[emailBtn的setTitle:obj2.customerEmail forState:UIControlStateNormal]。
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5]
[emailBtn setTitleColor:[[[的UIColor页头] initWithRed:0.121绿色:0.472蓝:0.823阿尔法:1]自动释放] forState:UIControlStateNormal]。
[emailBtn addTarget:自我行动:@selector(emailAction :) forControlEvents:UIControlEventTouchUpInside]。
[elementView addSubview:emailBtn]。
[emailBtn发行];
 

解决方案

设置contentHorizo​​ntalAlignment:

  emailBtn.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentLeft;
 

您可能还需要调整内容左边边界,否则文本将触左边框:

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

I need to display the email address from left side of a UIButton, but it is being positioned in the centre.

Is there any way to set the alignment to the left side of a UIButton?

This is my current code:

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];

解决方案

Set the contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

You might also want to adjust the content left inset otherwise the text will touch the left border:

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

这篇关于如何为左对齐设置的UIButton的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆