Force UILabel to display 2 lines
Asked Answered
B

7

7

I have UILabel that display single line if it have space for it.

However, I need to force it always display 2 lines, even if there is enough space for display it in 1 line.

Robust way is to reduce right spacing constraint and check that condition on all of available devices, but maybe there is an easier way?

To be clear, I want this:enter image description here

look like this: enter image description here

As I mentioned, now labels binded to superview by constraints.

Blindworm answered 11/7, 2016 at 13:12 Comment(6)
you need to fix width constant in AUTOLAYOUT to forcefully display two linesMillimeter
@MikeAlter how exactly?Blindworm
in autolayout set fix width and NSLineBreakByWordWrappingMillimeter
You might be best splitting your string by spaces and using two labels if its for an address.Weka
no it is not right way to assume there is only two worlds , you can splitMillimeter
label.numberoflines =0; label.lineBreakMode = NSLineBreakByWordWrapping ; remove constraints for textfield and set the frame size.width = 30. it automatically move to second lineRosmunda
C
12

Step 1. Set number of lines to your label to 2.
Step 2. Set "Vertical hugging priority" to High (1000)
Step 3. Right click on your label and connect with icon at left and choose "Center Vertically".
Step 4. Right click on your label and connect with icon at left and set "Horizontal Spacing".
Step 5. If that text is static then do as suggested Igor, else, just replace @" " with @"\n" after assigning text to that label. E.g.

NSString *text = @"Какой-то Справочник"; 
self.myLabel.text = [text stringByReplacingOccurrencesOfString:@" " withString:@"\n"];

Or just set self.myLabel.lineBreakMode = NSLineBreakByWordWrapping;

Corneille answered 11/7, 2016 at 13:29 Comment(1)
This is what op wants.Sauers
S
8

In InterfaceBuilder settings of UILabel text press option+enter between "Справочник" and "МКБ".

Or set label.text = @"Справочник\nМКБ";.

And, of course, set yourLabel.numberOfLines = 2 or 0 in IB or code.

Stockjobber answered 11/7, 2016 at 13:14 Comment(0)
U
4

Following code may helps you and you have to make label's height enough to cover two lines.

NSString *text = @"Какой-то Справочник";
self.Label.lineBreakMode = NSLineBreakByWordWrapping;
self.Label.numberOfLines = 2;
Unofficial answered 11/7, 2016 at 13:34 Comment(0)
P
2

You need to set the numberOfLines.

yourLabel.numberOfLines = 2
Pelagia answered 11/7, 2016 at 13:19 Comment(1)
This will not force two lines, but enable two lines.Witte
F
2

you can either force the label to present a two line text with inserting a "\n" in the location you want in the string.

Or you can set a constant width to the label to force it to stay in a constant width always (this will cause the label to present the text in two rows, as long as the text is bigger then the width). Remember to set the allowed rows number to 2.

Fairway answered 11/7, 2016 at 13:20 Comment(0)
T
2

If it is a fixed text then go for \n.

If it is a dynamic text then go for setting auto layouts with a fixed width and then numberoflines=0 and NSLineBreakByWordWrapping

Tache answered 11/7, 2016 at 13:30 Comment(0)
I
1

For swift 4.2, you can try using the following:

label.numberOfLines = 2
label.lineBreakMode = NSLineBreakMode.byWordWrapping
Incise answered 22/1, 2019 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.