149

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

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184

15 Answers15

202

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
sam_smith
  • 6,023
  • 3
  • 43
  • 60
RGML
  • 3,429
  • 2
  • 22
  • 18
110

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

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 1
    thanks...but I don't want to set it through IB. :( that's my last option of doing that... – Inder Kumar Rathore Apr 19 '11 at 08:39
  • 1
    @Inder Kumar Rathore: No problem. To set it in IB, you should click on the label, then in the Attributes Inspector, set Alignment to "Center". In my IB (XCode 4), it has 3 icons showing the alignment of text. Obviously, you should pick the middle one then. – Joetjah Apr 19 '11 at 08:51
  • can you please send me the screen shot... Is this the label of the Button or you have draged the UILabel from the library??? I suppose it UILabel not button's label – Inder Kumar Rathore Apr 19 '11 at 09:41
  • @Inder Kumar Rathore: Hey, check out the screenshot I added. I have indeed mistaken the kind of labels you use, I was still thinking on UILabel instead of the text in UIButton. The screenshot shows how to align on the UIButton through IB. – Joetjah Apr 19 '11 at 09:49
  • Yeah I have done it but it just align the button's content.... :( try multi line title you will find that... – Inder Kumar Rathore May 03 '11 at 05:39
  • UITextAlignmentCenter has been deprecated. Use NSTextAlignmentCenter now. – OC Rickard Dec 10 '13 at 20:24
  • @OCRickard Feel free to add explanation, I'm not actively working with Objective-C or iOS development anymore. – Joetjah Dec 11 '13 at 11:07
  • 1
    Wouldn't let me edit, but NSTextAlignmentCenter has taken the place of UITextAlignmentCenter. They mean exactly the same thing tho. https://developer.apple.com/library/iOS/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/c/tdef/NSTextAlignment – OC Rickard Dec 11 '13 at 21:05
99

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

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • For solution 3, you can just add `Line Breaking = Clip`, it will transform the text to multiline – finx May 17 '19 at 05:16
  • 1
    Solution 3 has problem, at least with Xcode 11.3.1: when I change button's title, it doesn't work. when I used `btn.titleLabel.textAlignment`, it works great. – Brian Hong Jan 24 '20 at 13:43
27

For UIButton you should use:-

[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
Maulik
  • 19,348
  • 14
  • 82
  • 137
Sirji
  • 554
  • 6
  • 9
16

For ios 8 and Swift

btn.titleLabel.textAlignment = NSTextAlignment.Center

or

btn.titleLabel.textAlignment = .Center

Eduardo Fiorezi
  • 570
  • 4
  • 11
14

For swift 4, xcode 9

myButton.contentHorizontalAlignment = .center
Shan Ye
  • 2,622
  • 19
  • 21
  • I set myButton.titleLabel?.numberOfLines = 0 and it caused the label within the button to center, so I was trying to figure out how to align the text to the left within the button and this worked, setting it to .left however. – maxcodes Feb 13 '19 at 02:08
13

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.

tyler53
  • 429
  • 1
  • 5
  • 16
7

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;
user1862723
  • 109
  • 1
  • 2
7

For Swift 4:

@IBAction func myButton(sender: AnyObject) {
    sender.titleLabel?.textAlignment = NSTextAlignment.center
    sender.setTitle("Some centered String", for:UIControlState.normal)
}
Milan Cermak
  • 7,476
  • 3
  • 44
  • 59
Grzegorz R. Kulesza
  • 1,324
  • 16
  • 10
4

UITextAlignmentCenter is deprecated in iOS6

Instead you can use this code:

btn.titleLabel.textAlignment=NSTextAlinmentCenter;

vrwim
  • 13,020
  • 13
  • 63
  • 118
4

For Swift 3.0

btn.titleLabel?.textAlignment = .center
Morten Gustafsson
  • 1,869
  • 2
  • 24
  • 34
3

Actually you can do it in interface builder.

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

Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35
3

Try Like this :

yourButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
Bhumika
  • 876
  • 7
  • 20
Bobby
  • 6,115
  • 4
  • 35
  • 36
2

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

1

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

For your reference

[buttonName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
btmanikandan
  • 1,923
  • 2
  • 25
  • 45