40

How do you center text line-by-line in a UIButton using Interface Builder? I am scouring the options and just don't see it. Here's the button:

button with text not centered

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
sdknewbie
  • 659
  • 2
  • 9
  • 13
  • The text should be centered by default. If you were to change it, you would do so under "Control > Alignment" which yours is set to be horizontally centered and vertically centered. What does your UIButton currently look like? – Sam Jan 27 '12 at 15:09
  • Everything is justified to the left. – sdknewbie Jan 27 '12 at 15:11
  • I think the real issue is that you're trying to center a multiline text in the UIButton... I'm going to change your title to reflect that. – Sam Jan 27 '12 at 15:15
  • Looks like the UIButton is centering the text, but it is centering it as a whole (i.e. the block of text is centered, not each individual line). I just tried this in interface builder. Are you wanting to center each line individually? – Sam Jan 27 '12 at 15:40
  • Yes. Isn't there some built in method to do this? It appears that it can't be done in the NIB. I can't be the first person to have this problem. – sdknewbie Jan 28 '12 at 15:33

7 Answers7

63

You can't set the text to be centered in your nib. But you can change the alignment in your code:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • +1 @sdknewbie have you tried this? This is an interesting answer as he's setting the text align of the label in the button. If this fixed things, please mark as the answer. If it didn't, leave a comment. – Sam Jan 28 '12 at 07:11
  • This idea looks intriguing, but it didn't work. I get a SIGABRT error. Here is the log entry. "unrecognized selector sent to instance." I am pretty sure I hooked the button up properly as the app compiles but crashes when I select the view with the button on it ... but I am the newbie! – sdknewbie Jan 28 '12 at 15:31
  • The log entry also told you both the selector that was unrecognized, and the class of the instance that received it. If you post that information too, we can be more helpful. – rob mayoff Jan 28 '12 at 18:05
  • Setting `textAlignment = UITextAligmentCenter` works in my test project. – rob mayoff Jan 28 '12 at 18:06
  • I am the newbie ... I had another error. This works like a charm! Rob is brilliant! I am humbled that you would grace me with your knowledge. Thanks! – sdknewbie Jan 28 '12 at 22:24
  • FYI, this works for a plain button title, but does not seem to work for an Attributed title. – Axeva Nov 28 '12 at 21:55
  • `UITextAlignmentCenter` is deprecated in iOS6, better use `self.myButton.titleLabel.textAlignment = NSTextAlignmentCenter;` – Esteve Aug 26 '14 at 11:23
  • 4
    for iOS8 (and Swift) you have to do use `NSTextAlignment.Center`. If you make your own button class you can do this in its init/constructor: `self.titleLabel?.textAlignment = NSTextAlignment.Center` – Soko Dec 31 '14 at 12:42
14

I know this is an old question, but I came across it in my own attempt to center the multi-line text of a UIButton in IB. What I found is that by default, when "title" is set to "plain" and "line break" is set to "word wrap" the longest line of the title text is centered and the other lines are left justified to this line (similar to the OP's screen capture).

In order to have all the lines centered properly, "title" needs to be changed to "attributed." This provides many more options to customize the appearance of the title text. Center each of the lines of text (you can now actually change the alignment for each line individually). Also be sure to set "line breaking" to "word wrap" under "more..." above the text. There seems to be a bug with how this line breaking option behaves, at least in Xcode 4.5 at this time, because the text on the button in IB will look incorrect, truncating everything except the first line. It seems the "word wrap" and truncate options are interpreted backwards in IB, but if you run the app it behaves correctly in the simulator.

Jeff Lockhart
  • 5,379
  • 4
  • 36
  • 51
11

Actually you can do it in interface builder.

Just set Title to "Attributed" and then choose center alignment.

@from comments : To wrap you need to set Line Break to Character Wrap or Word Wrap.

P.S : This might not render in xcode. But, it will work at runtime.

jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35
8

You can set the center multiline text in UIButton through storyboard.

This is how you make the text have two or more lines.

Set the below key Path at

Identity Inspector --> User defined runtime attributes --> add new key value pair with below

titleLabel.textAlignment - NSNumber - 1

and

 titleLabel.numberOfLines - NSNumber - 5 - or use "0" meaning "any number"

It will look like this:

enter image description here

Note that (2016) unfortunately it does not actually show the two or more lines of text in Storyboard (you see only the first one), but it works perfectly when you run in simulator or device.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Gupta Nishant
  • 169
  • 2
  • 3
  • it worked but i write as "titleLabel.numberOfLines".I don't know if it works without capital letters. Thanks. – EFE Aug 24 '15 at 17:10
4

For IB set Title to "Attributed" and select center alignment (like Alexander Danilov suggested)

But if you want to do it in code using Swift 4:

// center button text
yourButton.titleLabel?.textAlignment = .center 

// enable multiline if needed
yourButton.titleLabel?.numberOfLines = 0 
budiDino
  • 13,044
  • 8
  • 95
  • 91
2

Not all options are done using Interface Builder therefore you must do some of them by coding, and usually we do them inside the function viewDidLoad.

To center your text inside a button by code you can use the following:

button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

You can use the same technique to align the text to any direction, for example to the left:

button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

But this will take the text too much to the left and you might want to have some space before it keeping the alignment to the left, so you add an inset after the aligning code as follows:

button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

In this case we are pushing the text from the Y axis by 10 points. I say here points not pixels because as you know Apple uses the points technique to measure distances to be able to adapt easily between normal display and retina display (where retina is 2 times the normal one).

antf
  • 3,162
  • 2
  • 26
  • 33
  • -1 how does this help the guy asking the question (or anybody else)? – Sam Jan 28 '12 at 07:08
  • 3
    The question was asking about Interface Builder not about code and this answer serves this target by saying it can not be done. Moreover this answer gives an insight about where to do the coding (inside `viewDidLoad`). If the one who asked didn't know the coding way we expect to see him commenting on this answer and after that we will reply to his new comment by code. By this I see this answer helps the guy asking the question, and thank you for your comment. – antf Jan 28 '12 at 14:25
  • That's a fair response, however I can't change my vote unless you change your answer (forced constraint by SO after so many hours pass - who knew). While I would've liked to have seen an alternative solution, you're right that user didn't ask for one. – Sam Jan 30 '12 at 02:08
-3

I haven't tried it out yet, but I think a way to do it might be create a CGRect on top of your button, then use it as a frame, create a label, and then you can play with the label, set the textAlignment property to be UITextAlignmentCenter, and set the background color to be clear.

This works with uitableview but I don't know whether that will work for button. Hope this helps.

Raymond Wang
  • 1,484
  • 2
  • 18
  • 33