How to make UIButton's text alignment center? Using IB

IosObjective CUibuttonInterface Builder

Ios Problem Overview


I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
enter image description here

But I want like this one
enter image description here

I have given space in this but I don't want to do that. As it is not aligned exactly for some cases and I know there is a property of UILabel to set the alignment but I don't want to write a code for that.. just want to set everything from IB.

Thanks

Ios Solutions


Solution 1 - Ios

This will make exactly what you were expecting:

Objective-C:

 [myButton.titleLabel setTextAlignment:UITextAlignmentCenter];

For iOS 6 or higher it's

 [myButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

as explained in tyler53's answer

Swift:

myButton.titleLabel?.textAlignment = NSTextAlignment.Center

Swift 4.x and above

myButton.titleLabel?.textAlignment = .center

Solution 2 - Ios

Use the line:

myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

This should center the content (horizontally).

And if you want to set the text inside the label to the center as well, use:

[labelOne setTextAlignment:UITextAlignmentCenter];

If you want to use IB, I've got a small example here which is linked in XCode 4 but should provide enough detail (also mind, on top of that properties screen it shows the property tab. You can find the same tabs in XCode 3.x): enter image description here

Solution 3 - Ios

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

enter image description here

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

enter image description here

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

enter image description here

Solution 4 - Ios

For UIButton you should use:-

[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

Solution 5 - Ios

For ios 8 and Swift

or

Solution 6 - Ios

For those of you who are now using iOS 6 or higher, UITextAlignmentCenter has been deprecated. It is now NSTextAlignmentCenter

EXAMPLE: mylabel.textAlignment = NSTextAlignmentCenter; Works perfectly.

Solution 7 - Ios

For swift 4, xcode 9

myButton.contentHorizontalAlignment = .center

Solution 8 - Ios

Assuming that btn refers to a UIButton, to change a multi-line caption to be centered horizontally, you can use the following statement in iOS 6 or later:

self.btn.titleLabel.textAlignment = NSTextAlignmentCenter;

Solution 9 - Ios

For Swift 4:

@IBAction func myButton(sender: AnyObject) {
    sender.titleLabel?.textAlignment = NSTextAlignment.center
    sender.setTitle("Some centered String", for:UIControlState.normal)
}

Solution 10 - Ios

UITextAlignmentCenter is deprecated in iOS6

Instead you can use this code:

btn.titleLabel.textAlignment=NSTextAlinmentCenter;

Solution 11 - Ios

For Swift 3.0

btn.titleLabel?.textAlignment = .center

Solution 12 - Ios

Actually you can do it in interface builder.

You should set Title to "Attributed" and then choose center alignment.

Solution 13 - Ios

Try Like this :

yourButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

Solution 14 - Ios

You can do this from storyboard. Select your button. Set Line Break 'Word Wrap', Set your title 'Plain' to 'Attributed'. Select 'Center alignment'. This part is important => Click ...(More) Button. And select line breaking mode to 'Character Wrap'.

Solution 15 - Ios

UIButton will not support setTextAlignment. So You need to go with setContentHorizontalAlignment for button text alignment

For your reference

[buttonName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];

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
QuestionInder Kumar RathoreView Question on Stackoverflow
Solution 1 - IosRGMLView Answer on Stackoverflow
Solution 2 - IosJoetjahView Answer on Stackoverflow
Solution 3 - IosInder Kumar RathoreView Answer on Stackoverflow
Solution 4 - IosSirjiView Answer on Stackoverflow
Solution 5 - IosEduardo FioreziView Answer on Stackoverflow
Solution 6 - Iostyler53View Answer on Stackoverflow
Solution 7 - IosShan YeView Answer on Stackoverflow
Solution 8 - Iosuser1862723View Answer on Stackoverflow
Solution 9 - IosGrzegorz R. KuleszaView Answer on Stackoverflow
Solution 10 - Iosuser2223924View Answer on Stackoverflow
Solution 11 - IosMorten GustafssonView Answer on Stackoverflow
Solution 12 - IosAlexander DanilovView Answer on Stackoverflow
Solution 13 - IosBobbyView Answer on Stackoverflow
Solution 14 - IosMustafa KarakuşView Answer on Stackoverflow
Solution 15 - IosbtmanikandanView Answer on Stackoverflow