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
Swift UiButton how to left align the title?
Try this one
button.contentHorizontalAlignment = .left
How to alignment the text of button to the left?
You have to use contentVerticalAlignment
and contentHorizontalAlignment
of the button.
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);
Trouble left-aligning UIButton title (iOS/Swift)
Try this:
button.contentHorizontalAlignment = .left
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
UIButton title alignment not changing to right direction in RTL
Use right alignment from storyboard as
or from programmatically as
buttonShowOnMap.contentHorizontalAlignment = .left//For left alignment
buttonShowOnMap.contentHorizontalAlignment = .right//For right alignment
How to make UIButton's text alignment center? Using IB
Solution1
You can set the key path in the storyboard
Set the text to your multiline title e.g. hello
⌥ + ↵ multiline
You need to press ⌥ + ↵ to move text to next line.
Then add the key path
titleLabel.textAlignment
as Number
and value 1
, 1
means NSTextAlignmentCenter
titleLabel.numberOfLines
as Number
and value 0
, 0
means any number of lines
This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)
If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)
Subclass the UIButton to make the button designable:
import UIKit
@IBDesignable class UIDesignableButton: UIButton {}Assign this designable subclass to the buttons you're modifying:
- Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):
Solution2
If you want to write the code, then do the long process
1.Create IBOutlet
for button
2.Write code in viewDidLoad
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
Solution3
In newer version of xcode (mine is xcode 6.1) we have property attributed title
Select Attributed
then select the text and press centre option below
P.S. The text was not coming multiline for that I have to set the
btn.titleLabel.numberOfLines = 0
Related Topics
How to Add Spacing Between Uitableviewcell
Ios Simple Tcp Connection Example
Test iOS App on Device Without Apple Developer Program or Jailbreak
Adjust Uilabel Height Depending on the Text
Ios Upload Image and Text Using Http Post
Nsfilemanager.Defaultmanager().Fileexistsatpath Returns False Instead of True
How to Add Uitableview Within a Uitableviewcell
Conditional Binding: If Let Error - Initializer For Conditional Binding Must Have Optional Type
Converting Storyboard from Iphone to Ipad
How to Record Video of Front and Back Camera At a Time in Ios
React Native App Crashes on Launch Screen on Device
How to Debug 'Unrecognized Selector Sent to Instance' Error
How to Apply a Perspective Transform to a Uiview
How to Use Nsjsonserialization