零零乙 2010-05-04 11:56 采纳率: 33.3%
浏览 532
已采纳

如何将 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条回答 默认 最新

  • 笑故挽风 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条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀