我如何设置一个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];
234332 次浏览

设置contentthorizontalalign:

// 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);
@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];

希望这有助于....

艾尔

这里解释了如何做到这一点,为什么它是这样工作的: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html < / p >

使用emailBtn.titleEdgeInsetscontentEdgeInsets更好,以防你不想改变按钮内的整个内容位置。

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

UIButton in IB

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

enter image description here

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

在Swift 3+中:

button.contentHorizontalAlignment = .left

对于Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

如果有人需要,这可以帮上忙。

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

斯威夫特4 +

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

斯威夫特5.0Xcode 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

试一试

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
< h1 id = " swiftui-8t9g " > SwiftUI h1 > < /

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

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

如果你使用button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10),你会得到一个声明'contentEdgeInsets' was deprecated in iOS 15.0: This property is ignored when using UIButtonConfiguration的警告。另一种解决方案是:

iOS 15.0 +

    var button: UIButton = {
let button = UIButton(configuration: .filled())


button.configuration?.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 20, bottom: 16, trailing: 20)
button.contentHorizontalAlignment = .leading
        

return button
}()