Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with multi-line titleLabel in InterfaceBuilder

Tags:

I've been wondering, is there a nice way to make UIButtons with multi-line titleLabel in Interface Builder? I've found none and puttin a separate UILabel on top of each button and tracking it's color on press and other events is not very straightforward.

Maybe there are better alternatives done in code if there's no such way in IB?

like image 474
Sergey Grischyov Avatar asked Sep 06 '13 14:09

Sergey Grischyov


People also ask

How do you make a UIButton multiline?

To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .

What is a UIButton?

A control that executes your custom code in response to user interactions.

What is button in iOS swift?

It is a control that enables the user to interact with the application. It is used to trigger the events performed by the user. It executes the custom code in response to user interactions. class UIButton : UIControl.


1 Answers

Code:

To allow multiple line you can use:

button.titleLabel.lineBreakMode = UILineBreakModeWordWrap; button.titleLabel.textAlignment = UITextAlignmentCenter; [button setTitle: @"Line1\nLine2" forState: UIControlStateNormal]; 

In iOS 6, UILineBreakModeWordWrap and UITextAlignmentCenter are deprecated, so use:

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

Interface Builder:

  • In interface builder select UIButton
  • on the right side Utilities pane under Attributes Inspector, you'll see an option for Line Break
  • Choose Word Wrap
like image 100
Mrunal Avatar answered Sep 21 '22 06:09

Mrunal