iOS multiline label in Interface builder
Asked Answered
S

11

143

How can I make a multiline UILabel in interface builder for iOS? I tried the UITextView but it didn't quite suit my needs.

How can I add multiline (text) in label?

Semiweekly answered 2/7, 2011 at 14:3 Comment(4)
Can you explain why id did not suite your needs? TextView with read-only is same as a multiline label.Vannie
There doesn't seem to be highlighted text color property.Semiweekly
For others visiting this question, dont follow accepted answer of setting lines to 5 or n. Instead set it to 0(Sort of infinity). Check vijay's answer below.Fairy
#990721Moynihan
S
190

You can use numberOfLines property which defines maximum number of lines a label can have. By default, it's 1. Setting it to 0 means the label will have unlimited lines.

You can do it in code:

textLabel.numberOfLines = 5 // for example

Or in Interface Builder:

Shantell answered 2/7, 2011 at 14:27 Comment(7)
I tried, it works when I am writing the label, but when I press enter to accept it just all goes on one line.Semiweekly
And what's the width of your label?Shantell
Ok, I got it now, increased the height, that made it. Thanks!Semiweekly
You can set Lines to 0 for unlimited lines.Ova
See also this answer which explains how to type a multi-line label into IB: https://mcmap.net/q/79858/-multiple-lines-of-text-in-uilabelOva
Setting the lines to 3 , align center and increase height did it thanksStrife
BTW: Use Option+Enter for a new lineVerbal
F
126

Hit Control+Enter to add a line in UILabel in Interface Builder/Storyboard.

Flinn answered 9/2, 2014 at 2:20 Comment(4)
Nice one. But "Q" isn't necessary I guessStyptic
Spot on ! Exactly what i have been trying to do. No messing up with codeBork
@Oliver Logic it does not quit Xcode... You haven't read correctly. He said CONTROL+Q+ENTER (and not CMD+Q).. But Rendel is right only CONTROL+ENTER suffices here.Detour
@Logic it does not quit Xcode... You haven't read correctly. He said CONTROL+Q+ENTER (and not CMD+Q).. But Rendel is right only CONTROL+ENTER suffices here.Detour
T
48

Thanks AppleVijay!

Also to call sizeToFit, like this:

label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit];

The height will be automatically computed.

Tremml answered 18/4, 2012 at 14:34 Comment(3)
Note: UILineBreakModeWordWrap is deprecated as of iOS6. Use [label setLineBreakMode:NSLineBreakWordWrapping]; instead.Cuisse
Missing "By". label.lineBreakMode = NSLineBreakByWordWrappingRelaxation
use NSLineBreakMode.ByWordWrapping instead of UILineBreakModeWordWrap for swiftHortative
U
12

set width of label as u needed small then use IB to set line breaks to word wrap

or use with code like this

I found a solution.

One just has to add the following code:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Usanis answered 2/7, 2011 at 14:26 Comment(0)
C
8

Set number of lines zero for dynamic text information, it will be useful when your text are varying.

Programatically (Swift 3)

var label = UILabel()
let stringValue = "iOS\nmultiline\nlabel\nin\nInterface\nbuilder"
label.text = stringValue
label.numberOfLines = 0 // Set 0, if number of lines not specified.
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame

Using Inetrface Builder

enter image description here

Note: It is not required to set Minimum Font Scale, but nice to have a minimum scale factor to fit text into label frame.

Celio answered 4/3, 2017 at 5:36 Comment(2)
How can I make label height variable based on text in it ?Hambley
@Hambley - The 'Autoshrink' dropdown has another value in the list - Miminum font size, which may solve your problem.Celio
L
2

Number of lines is visible in IB with Plain UILabels set lines field as 0 . It will create multiple lines as per the provided space to the label.

Lallage answered 13/4, 2018 at 12:23 Comment(0)
R
1

In iOS7 (Xcode5) you shold set the lines of UILabel to 0 for unlimited multiple input in storyboard.
The most important is to set the height of the UILabel can hold the lines of input you are going to set.

Rocky answered 8/10, 2014 at 3:11 Comment(0)
P
0
textLabel.lineBreakMode = UILineBreakModeWordWrap;

textLabel.numberOfLines = 0;

CGSize size =  [[[arrNewsFeed objectAtIndex:row] objectForKey:@"c"]  sizeWithFont:[UIFont systemFontOfSize:14.0]  constrainedToSize:CGSizeMake(188, CGFLOAT_MAX)
                                                                     lineBreakMode:NSLineBreakByTruncatingTail];

textLabel.frame = (CGRect){.origin = cell.lblNewsDescription.frame.origin, .size = size};
Polytrophic answered 8/10, 2014 at 5:14 Comment(0)
S
0

If you set the numberOfLines property equal to 0, the label will automatically adjust to the required number of lines of the given text.

Simultaneous answered 22/3, 2018 at 18:6 Comment(0)
P
0

I had been struggling with this for a long time. I just wanted my label text to appear on the second line. But whatever I do, it would just overflow the UILabel box. For me, changing the autoresizing in size inspector worked. Simple fix.

May be someone might find it helpful who is looking for something similar.

Prom answered 13/5, 2020 at 18:57 Comment(0)
G
-2

For X-Code 7.2

  1. -- Select UILabel

  2. -- Attributes inspector

  3. -- Text - Select Attributed

After this you can see some more attribute you can add into you label, in which you can also find number of Lines. Which make your label multiline.

X-Code Image for a multiline UILabel

Golly answered 7/4, 2016 at 9:21 Comment(1)
Number of lines is visible in IB with Plain UILabels.Minelayer

© 2022 - 2024 — McMap. All rights reserved.