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

IosObjective CXcodeCocoa TouchUibutton

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

Set the contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = .left;

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

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

Solution 2 - Ios

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

Solution 3 - Ios

In Swift 3+:

button.contentHorizontalAlignment = .left

Solution 4 - Ios

Swift 4+

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

Solution 5 - Ios

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

Solution 6 - Ios

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

Solution 7 - Ios

Solution 8 - Ios

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

Solution 9 - Ios

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

Solution 10 - Ios

For Swift 2.0:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  

This can help if any one needed.

Solution 11 - Ios

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

Solution 12 - Ios

Try

button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

Solution 13 - Ios

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)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMadan MohanView Question on Stackoverflow
Solution 1 - Iosuser121301View Answer on Stackoverflow
Solution 2 - Iosn8trView Answer on Stackoverflow
Solution 3 - IosVincentView Answer on Stackoverflow
Solution 4 - IosMohammad Zaid PathanView Answer on Stackoverflow
Solution 5 - IosbhavikView Answer on Stackoverflow
Solution 6 - IosGabrielView Answer on Stackoverflow
Solution 7 - IosemkakaView Answer on Stackoverflow
Solution 8 - IosdangView Answer on Stackoverflow
Solution 9 - IosAl-Noor LadhaniView Answer on Stackoverflow
Solution 10 - IosSuraj SonawaneView Answer on Stackoverflow
Solution 11 - IosNareshView Answer on Stackoverflow
Solution 12 - IosGevView Answer on Stackoverflow
Solution 13 - IosTamás SengelView Answer on Stackoverflow