bug^君 2010-05-04 11:56 采纳率: 25%
浏览 310
已采纳

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

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

转载于:https://stackoverflow.com/questions/2765024/how-to-set-the-title-of-uibutton-as-left-alignment

  • 写回答

10条回答 默认 最新

  • python小菜 2010-05-04 12:22
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装