How can I set the title of a UIButton as left-aligned?
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);
Swift UiButton how to left align the title?
Try this one
button.contentHorizontalAlignment = .left
Trouble left-aligning UIButton title (iOS/Swift)
Try this:
button.contentHorizontalAlignment = .left
How to left align title on button using titleLabel property?
You cannot really / should not really use the titleLabel
property for this since the UIButton
decides how to layout the label, where to position it and with what size, etc. If the button decides to put the label in the center and size it to the minimal dimensions it needs to display the text then changing any alignment property of the label will have no effect.
button.contentHorizontalAlignment = .left
is the way to go. Do not change the integrated label, but the button.
How to change Button Title Alignment in Swift?
Use contentHorizontalAlignment
.You have to use UIControlContentHorizontalAlignment.Left
.You need to usehorizontal
not vertical
.
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
Swift 3.x
btn.contentHorizontalAlignment = .left
Setting title alignment to left on button
The text is being left-aligned. The problem is that the title label (within which the text is left-aligned) is itself located in the middle of the button!
So, what you want to do is adjust the position of the label within the button. There are various ways to do this. For example, look at the button's contentHorizontalAlignment
.
How to center attributed title in UIButton
This is because the text is an attributed String, you will need to align the text after it has been set.
Solution for you will be:
aButton.setAttributedTitle(title, for: .normal)
aButton.titleLabel?.textAlignment = .center
By setting attributed String, instead of String, you are clearing all the button's text property.
Nicer solution will be to add alignment attribute into the attributed String
Related Topics
Use of Unresolved Identifier' in Swift
Afnetworking 3.0 Migration: How to Post with Headers and Http Body
Tablefooterview Property Doesn't Fix the Footer at the Bottom of the Table View
Xcode Nsmanagedobject Subclass Contains Optionals When They Are Marked as Non-Optional
Alternative to Dtsendsignalflag to Identify Key Events in Instruments
Callback Method If User Declines Push Notification Prompt
Drawing Class Drawing Straight Lines Instead of Curved Lines
Nsurlconnection Sendasynchronousrequest Can't Get Variable Out of Closure
Xcode Is Looking for Core Data Entity Names with Dot; Not Compiling
Foreign Key Relationship Mapping with Restkit
Changing Vc Issue in Swift. How to Pass Data Between Views in Tab Bar Controller
What Is the '' Is or the ':' at the Equation When Coding in Swift
How to Log a Method's Execution Time Exactly in Milliseconds
Custom Installed Font Not Displayed Correctly in Uilabel
Using Multiple Storyboards in iOS
Automatic Preferred Max Layout Width Is Not Available on iOS Versions Prior to 8.0