关于objective c:如何将UIButton的标题设置为左对齐?

How to set the title of UIButton as left alignment?

我需要显示UIButton左侧的电子邮件地址,但它位于中间。

是否有任何方法可以设置到UIButton左侧的对齐?

这是我当前的代码:

1
2
3
4
5
6
7
8
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];

设置ContentHorizontalSignment:

1
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

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

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


如果不想在代码中进行调整,也可以使用接口生成器。在这里,我左对齐文本,并将其缩进一些:

UIButton in IB

别忘了,您也可以在按钮中对齐图像。

enter image description here


在SWIFT 3 +:

1
button.contentHorizontalAlignment = .left

1
2
3
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

斯威夫特4 +

1
2
3
button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .top
button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

如果您不想更改按钮内的整个内容位置,那么使用emailBtn.titleEdgeInsetscontentEdgeInsets更好。


下面解释了如何实现这一目标以及为什么它会如此有效:http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html


在Xcode 8.2.1中,在接口生成器中,它移动到:enter image description here


@dyingcactus的代码有一个小错误。以下是将uiLabel添加到uiButton以对齐按钮文本以更好地控制按钮"标题"的正确解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NSString *myLabelText = @"Hello World";
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

// position in the parent view and set the size of the button
myButton.frame = CGRectMake(myX, myY, myWidth, myHeight);

CGRect myButtonRect = myButton.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: myButtonRect];  
myLabel.text = myLabelText;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];  
myLabel.textAlignment = UITextAlignmentLeft;

[myButton addSubview:myLabel];
[myLabel release];

希望这有帮助…


斯威夫特2:

1
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

如果有人需要,这会有所帮助。


在Swift 5.0和Xcode 10.2中

你有两种方法接近

1)直接进近

1
btn.contentHorizontalAlignment = .left

2)sharedclass示例(只写一次,使用每个软件)

这是您的共享类(像这样,您可以访问所有组件属性)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit

class SharedClass: NSObject {

    static let sharedInstance = SharedClass()

    private override init() {

    }
}

//UIButton extension
extension UIButton {
    func btnProperties() {
        contentHorizontalAlignment = .left
    }
}

在你的视图控制器中调用如下

1
button.btnProperties()//This is your button