ChatGPT解决这个技术问题 Extra ChatGPT

How can I set the title of a UIButton as left-aligned?

I need to display an email address on the left side of a UIButton, but it is being positioned to 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];
For Swift 5.1 you can look my answer stackoverflow.com/questions/2765024/…

G
Guntis Treulands

Set the contentHorizontalAlignment:

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

You might also want to adjust the content left inset otherwise the text will touch the left border:

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

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

Keep in mind that you can also set both of these properties from Interface Builder.
so it was number 9000 from me, thanx , useful answer
We're too lazy to search the documentation. What would we do without SO!
Goes to show SO is better documented than Apple document.
UIControlContentHorizontalAlignmentLeading and UIControlContentHorizontalAlignmentTrailing were added in iOS 11
n
n8tr

You can also use interface builder if you don't want to make the adjustments in code. Here I left align the text and also indent it some:

https://i.stack.imgur.com/BdUIE.png

Don't forget you can also align an image in the button too.:

https://i.stack.imgur.com/zF8Pe.png


This helped me out a ton, can't believe I didn't realize that's what those controls were for.
Thanks, It helped me. I was not able to find out edge insets for button title without the help of arrow indication in your sample pictures.
in xcode 8.2.1 it moved to other place see my answer below
How to do that programmatically in swift? I want align image on the right, and title on the left side???
F
Federico Zanetello

In Swift 3+:

button.contentHorizontalAlignment = .left

What is the difference between .leading and .left value of contentHorizontalAlignment?
@jamryu that in case of languages where you write from right to left (arabic I think..?) .leading will have same effect as .right.
M
Mohammad Zaid Pathan

Swift 4+

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

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

s
shanet

Using emailBtn.titleEdgeInsets is better than contentEdgeInsets, in case you don't want to change the whole content position inside the button.


e
emkaka
d
dang

https://i.stack.imgur.com/qFEYr.png


A
Al-Noor Ladhani

There is a small error in the code of @DyingCactus. Here is the correct solution to add an UILabel to an UIButton to align the button text to better control the button '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];

Hope this helps....

Al


S
Suraj Sonawane

For Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

This can help if any one needed.


N
Naresh

In Swift 5.0 and Xcode 10.2

You have two ways to approaches

1) Direct approach

btn.contentHorizontalAlignment = .left

2) SharedClass example (write once and use every ware)

This is your shared class(like this you access all components properties)

import UIKit

class SharedClass: NSObject {

    static let sharedInstance = SharedClass()

    private override init() {

    }
}

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

In your ViewController call like this

button.btnProperties()//This is your button

G
Gev

Try

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

This worked for me, contentHorizontalAlignment didn't, thanks.
T
Tamás Sengel

SwiftUI

You should change the alignment property of the .frame modifier applied to the Text. Additionally, set the multiline text alignment to .leading.

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