By Madan Mohan

2010-05-04 11:56:19 8 Comments

I need to display the email address from left side of a UIButton, but it is being positioned in 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];


@iOS 2019-04-29 05:10:30

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

@Zaid Pathan 2017-09-22 11:37:28

Swift 4+

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

@dang 2017-01-27 12:55:58

in xcode 8.2.1 in the interface builder it moves to:enter image description here

@Vincent 2015-09-01 06:06:46

In Swift 3+:

button.contentHorizontalAlignment = .left

@Suraj Sonawane 2015-12-02 12:31:39

For Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

This can help if any one needed.

@bhavik 2014-08-30 04:58:05

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

@n8tr 2014-03-29 00:13:15

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:

UIButton in IB

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

enter image description here

@Ethan Parker 2015-05-18 17:54:28

This helped me out a ton, can't believe I didn't realize that's what those controls were for.

@Ashok 2015-07-09 09:55:04

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.

@dang 2017-01-27 12:56:25

in xcode 8.2.1 it moved to other place see my answer below

@Visal Sambo 2018-08-26 15:52:59

How to do that programmatically in swift? I want align image on the right, and title on the left side???

@Gabriel 2013-08-30 04:42:04

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

@emkaka 2013-03-06 17:02:52

@Al-Noor Ladhani 2011-01-03 22:59:43

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....


@user121301 2010-05-04 12:22:49

Set the contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

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

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

@Mihai Damian 2013-02-01 08:49:24

Keep in mind that you can also set both of these properties from Interface Builder.

@Logic 2015-05-25 13:02:09

so it was number 9000 from me, thanx , useful answer

@Kedar Paranjape 2015-11-24 06:48:33

We're too lazy to search the documentation. What would we do without SO!

@GeneCode 2016-10-13 10:46:12

Goes to show SO is better documented than Apple document.

@Ricardo 2017-03-15 11:04:27

Is it possible to use Leading or Trailing rather than Right or Left? (for localisation purposes)

@Cal Stephens 2018-01-06 01:48:01

UIControlContentHorizontalAlignmentLeading and UIControlContentHorizontalAlignmentTrailing were added in iOS 11

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] How do I create a basic UIButton programmatically?

16 Answered Questions

[SOLVED] Set UIButton title UILabel font size programmatically

5 Answered Questions

[SOLVED] How can I change UIButton title color?

91 Answered Questions

22 Answered Questions

[SOLVED] How to stop unwanted UIButton animation on title change?

3 Answered Questions

[SOLVED] UIButton title text color

18 Answered Questions

2 Answered Questions

[SOLVED] Custom back button view

1 Answered Questions

[SOLVED] UITableViewCell text push to viewController

4 Answered Questions

[SOLVED] Create elements programmatically and reuse code

Sponsored Content