UIButton title alignment and multiline support

IphoneIosCocoa TouchUikitUibutton

Iphone Problem Overview


How do I set the title of a UIButton to be left-aligned, and how can I show multiple lines of text in a UIButton?

Iphone Solutions


Solution 1 - Iphone

Here's the code to do the UIButton alignment in code too: -

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

Solution 2 - Iphone

To set the alignment of a UIButton, look in the Attributes Inspector in Interface Builder for the button. There's a section called Alignment. Set it to left. Or right, or whatever you want.

enter image description here

To do it in code, use the contentHorizontalAlignment property of UIControl. You can set it to any of these properties:

UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

None of these options look particularly good (as you can see above), and you might have might have more luck using the contentEdgeInsets property of UIButton to reposition the content.

To set a multiline title on a UIButton, check this forum post which describes changing the button label.

Or you can use this code to quickly make a 2 line button:

myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:@"Siegfried\nRoy" forState:UIControlStateNormal];

enter image description here

Raju, don't forget to mark questions as accepted.

Solution 3 - Iphone

Actually, you can set the lineBreakMode to UILineBreakModeWordWrap or UILineBreakModeCharacterWrap. This breaks the text into multiple lines if necessary.

Solution 4 - Iphone

Try this

    myButton.titleLabel.textAlignment = UITextAlignmentLeft;
    myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    myButton.titleLabel.numberOfLines = 0;

Solution 5 - Iphone

To add a Paragraph effect you can change the size of title.

For alinea effect

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
QuestionRajuView Question on Stackoverflow
Solution 1 - IphoneIan KershawView Answer on Stackoverflow
Solution 2 - Iphonenevan kingView Answer on Stackoverflow
Solution 3 - IphoneMarcView Answer on Stackoverflow
Solution 4 - IphonerakeshNSView Answer on Stackoverflow
Solution 5 - IphoneAnthoneView Answer on Stackoverflow