How to Change Button Title Alignment in Swift

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

Sample Image

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.

Sample Image

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

Sample Image

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)

  1. Subclass the UIButton to make the button designable:

    import UIKit
    @IBDesignable class UIDesignableButton: UIButton {}

  2. Assign this designable subclass to the buttons you're modifying:

Showing how to change the class of the button using Interface Builder


  1. Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):

Comparing the designable and default button in Interface Builder



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



Sample Image



Related Topics



Leave a reply



Submit