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

时间:2010-05-04 11:56:20

标签: objective-c uibutton

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

14 个答案:

答案 0 :(得分:1500)

设置contentHorizo​​ntalAlignment:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

您可能还想调整内嵌左边的内容,否则文字会触及左边框:

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

答案 1 :(得分:116)

如果您不想在代码中进行调整,也可以使用界面构建器。 在这里,我左对齐文本并将其缩进一些:

UIButton in IB

不要忘记你也可以在按钮中对齐图像。:

enter image description here

答案 2 :(得分:30)

在Swift 3 +中:

button.contentHorizontalAlignment = .left

答案 3 :(得分:13)

UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

答案 4 :(得分:9)

Swift 4 +

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

答案 5 :(得分:8)

这里解释了如何操作以及它为什么这样做: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

答案 6 :(得分:8)

如果您不想更改按钮内的整个内容位置,则使用emailBtn.titleEdgeInsets优于contentEdgeInsets

答案 7 :(得分:6)

在xcode 8.2.1的界面构建器中,它移动到:enter image description here

答案 8 :(得分:4)

@DyingCactus的代码中有一个小错误。 以下是将UILabel添加到UIButton以对齐按钮文本以更好地控制按钮“title”的正确解决方案:

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

希望这会有所帮助......

的Al

答案 9 :(得分:2)

对于Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

如果需要的话,这可以提供帮助。

答案 10 :(得分:0)

Swift 5.0 Xcode 10.2

您有两种方法

1)直接接触

btn.contentHorizontalAlignment = .left

2) SharedClass 示例(编写一次并使用每种商品)

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

import UIKit

class SharedClass: NSObject {

    static let sharedInstance = SharedClass()

    private override init() {

    }
}

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

在您的 ViewController 调用中

button.btnProperties()//This is your button

答案 11 :(得分:0)

尝试

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

答案 12 :(得分:0)

Swift 5.1

self.btnPro.titleLabel?.textAlignment = .left

答案 13 :(得分:-1)

SwiftUI

您应该更改应用于文本的 .frame 修饰符的对齐属性。此外,将多行文本对齐方式设置为 .leading

Button {
    // handler for tapping on the button
} label: {
    Text("Label")
        .frame(width: 200, alignment: .leading)
        .multilineTextAlignment(.leading)
}