gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:12:21 26 4
gpt4 key购买 nike

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

最佳答案

设置contentHorizo​​ntalAlignment:

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

您可能还想调整左插入的内容,否则文本将触及左边框:

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

关于ios - 如何将 UIButton 的标题设置为左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2765024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com