How do you add multi-line text to a UIButton?

199,085

Solution 1

For iOS 6 and above, use the following to allow multiple lines:

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to 
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];

For iOS 5 and below use the following to allow multiple lines:

button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
// you probably want to center it
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];

2017, for iOS9 forward,

generally, just do these two things:

  1. choose "Attributed Text"
  2. on the "Line Break" popup select "Word Wrap"

Solution 2

The selected answer is correct but if you prefer to do this sort of thing in Interface Builder you can do this:

pic

Solution 3

If you want to add a button with the title centered with multiple lines, set your Interface Builder's settings for the button:

[here]

Solution 4

For IOS 6 :

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;

As

UILineBreakModeWordWrap and UITextAlignmentCenter

are deprecated in IOS 6 onwards..

Solution 5

To restate Roger Nolan's suggestion, but with explicit code, this is the general solution:

button.titleLabel?.numberOfLines = 0
Share:
199,085
Owain Hunt
Author by

Owain Hunt

Updated on November 23, 2021

Comments

  • Owain Hunt
    Owain Hunt over 2 years

    I have the following code...

    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:targetButton.bounds];
    buttonLabel.text = @"Long text string";
    [targetButton addSubview:buttonLabel];
    [targetButton bringSubviewToFront:buttonLabel];
    

    ...the idea being that I can have multi-line text for the button, but the text is always obscured by the backgroundImage of the UIButton. A logging call to show the subviews of the button shows that the UILabel has been added, but the text itself cannot be seen. Is this a bug in UIButton or am I doing something wrong?

  • Owain Hunt
    Owain Hunt about 15 years
    I'm aware of the title property, but as far as I can tell it is impossible to set it to use more than one line, hence this approach. If I disable the backgroundImage, my UILabel show up, which to me suggests a bug in either bringSubviewToFront, or UIButton itself.
  • Christopher Pickslay
    Christopher Pickslay over 13 years
    UIButton.lineBreakMode was deprecated in 3.0, so that's no longer a good option.
  • Wienke
    Wienke over 11 years
    lineBreakMode is deprecated only as a direct property of UIButton. We are directed to "Use the lineBreakMode property of the titleLabel instead." I edited Valerii's answer accordingly. It's still a good option.
  • Aaron Brager
    Aaron Brager over 11 years
    Please note these assignments are deprecated in iOS 6. See the answer below by @NiKKi for the updated syntax.
  • The dude
    The dude over 10 years
    Just so people know: this does not work if you're using NSAttributedString
  • JakubKnejzlik
    JakubKnejzlik almost 10 years
    Please, if you suggest this kind of ugly approach at least don't make mistakes in your code example :). The label shouldn't have the same center as button if it's a subview. Use myLabel.frame = button.bounds; or myLabel.center = cgPointMake(button.frame.size.with*0.5,button.frame.size.hei‌​ght*0.5)
  • Becca Royal-Gordon
    Becca Royal-Gordon almost 10 years
    @GrizzlyNetch I'm specifically advising not to add it as a subview of the button. addSubview: here adds it to button's superview, thus making it a sibling of the button, so matching their centers is correct.
  • JakubKnejzlik
    JakubKnejzlik almost 10 years
    Ah, my mistake! You are right, I've missed the "small" fact it's in the superview :D ... sorry :)
  • RyJ
    RyJ over 9 years
    Actually, just add button.titleLabel.numberOfLines = 0; That will make the lines unlimited. And button.titleLabel.textAlignment = NSTextAlignmentLeft; if you want left justified text as the title is centered by default.
  • Robert
    Robert over 9 years
    In swift button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
  • Alex Cio
    Alex Cio about 9 years
    Inside NSText.h in the Foundation there is no deprecated added. Its available from 6.0 but not deprecated.
  • Alex Cio
    Alex Cio about 9 years
    @jessecurry I don't see any difference to this snippet compared with the one from NiKKi..... and NSTextAlignmentCenter isn't deprecated like NiKKi tells.
  • mjswensen
    mjswensen over 8 years
    Thanks @Robert for the swift solution. Can also use inference and omit the NSLineBreakMode, like so: button.titleLabel?.lineBreakMode = .ByWordWrapping.
  • Rory McCrossan
    Rory McCrossan over 8 years
    Can you add a textual description of how to achieve this. An image is great, but if it becomes unavailable your answer will be useless.
  • user5440039
    user5440039 over 8 years
    @RoryMcCrossan Hi, i added an image above. Can you view it? Please note, Xcode 7 is still buggy so button title might disappear. However, once you run the app you will get the desired result.
  • jcity
    jcity over 8 years
    In swift: button.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping button.titleLabel?.textAlignment = NSTextAlignment.Center
  • Avi Cohen
    Avi Cohen about 8 years
    As of Xcode 7.3, trying to center the text in IB doesn't work so you'll need to IBOutlet the button and in the viewDidLoad add: button.titleLabel.textAlignment = UITextAlignmentCenter;
  • DrMickeyLauer
    DrMickeyLauer over 7 years
    Note that, if you want to use AutoLayout, UIButton has a bug that prevents the intrinsic content size to work properly with edge insets. To fix that, please refer to stackoverflow.com/questions/17800288/… where a good solution is presented.
  • RainCast
    RainCast over 7 years
    This is the best answer in this thread, actually. Works immediately and shows centered text from IB. Thanks!
  • Fattie
    Fattie over 7 years
    @user5440039 Thanks for the great answer. There's no need to add a textual description.
  • Adam Waite
    Adam Waite almost 6 years
    Thanks to my 24 year old self for writing this answer, from 28 year old self.
  • AnthoPak
    AnthoPak almost 6 years
    And to stay with the Interface Builder only setup, one should set titleLabel.textAlignment with Number value to 1 in User Defined Runtime Attributed to make the text centered
  • Dani
    Dani over 5 years
    Thanks to your 28 year old self from my 24 year old self!
  • tounaobun
    tounaobun about 5 years
    myButton.titleLabel.textAlignment = NSTextAlignmentCenter statement is also needed.
  • XLE_22
    XLE_22 almost 5 years
    For your record, using a multi-line title label with an attributed string needs creating constraints to wrap perfectly this text inside the button: it's not as easy as a simple string.
  • Pablo Blanco
    Pablo Blanco almost 5 years
    That it is not exactly what is asked, this solution would be a bad way to implement the question since the element button has a title.
  • Becca Royal-Gordon
    Becca Royal-Gordon almost 5 years
    This was reasonable advice in iPhone OS 2.0. There are much better ways to do it by now. :^)
  • ShadeToD
    ShadeToD over 4 years
    Works perfect in swift 5. I also added my function to center text in button. func centerTextInButton() { guard let titleLabel = titleLabel else { return } titleLabel.textAlignment = .center }
  • Bill Chan
    Bill Chan about 4 years
    I found adding constraint is not necessary, just set the text after lineBreakMode/textAlignment/numberOfLines are set. And also set titleEdgeInsets for top and bottom inset.
  • Amir Khorsandi
    Amir Khorsandi about 4 years
    @BillChan unfortunately that doesn’t work in all cases
  • androidguy
    androidguy almost 4 years
    As written, these constraints result in auto-layout conflict every time. Although I am not yet 100% sure of the importance of the use of inequality constraints, I do know that 2 of the constraints are written incorrectly: the bottom and right edges should use relatedBy: .lessThanOrEqual and flip the constant negative constant: -contentEdgeInsets.{bottom|right}
  • user2330482
    user2330482 almost 4 years
    Thank you - I had a ... in the last line due to an invisible character so I have added .lineBreakMode = .byClipping
  • Mr. Disability
    Mr. Disability over 3 years
    Thanks to your 30-year-old self from my 27-year-old self!
  • PhillipJacobs
    PhillipJacobs over 3 years
    Lol, this solution is so perfect. Thank you!
  • pableiros
    pableiros over 2 years
    ... and how do you implement multiline functionality using UIButtton.Configuration?