5

I have a UILabel and I want to show some text in this label. I want to increase the label width at most 70% of the full screen of my device. If text length of that label doesn't fit this 70% of size then the label automatically goes to the next line as long as the text length. Every time the label length cross the 70% width of main screen then lines break as well. I have tried several ways but unable to solve yet. Please help me to solve this.

Thanks in advance;

3
  • Please add some code and design. Commented Mar 14, 2017 at 5:11
  • Set numberOfLines to 0 for UILabel Commented Mar 14, 2017 at 5:18
  • Possible duplicate of How to do multiline UILabel in ios? Commented Mar 14, 2017 at 5:19

3 Answers 3

5

Drag a label to your storyboard and add top and leading constraints to it.

Now select the label and control drag to the view holding the label (in your case view of ViewController) you will see the pop up and then select equal width

Xcode storyboard screenshot

Now your Label's width is equal to your view's width :) That's not you want you want ur label width to be 70% of your view. So select the equal constraint of label, go to property inspector and change the multiplier to 0.7

Xcode storyboard screenshot

Now your label width is 70% of your view!

But you don't want it to be 70% always. It can be at max 70% of screen, so now change the relationship of constraint from being equal to less than or equal to.

Xcode storyboard screenshot

select label and change number of lines to 0.

That's it :) have fun :)

Sample O/P:

When text is short - vs - long:

example when text is small - - - example when text is more

EDIT:

Not using a storyboard? Not a problem; write the same constraint programmatically and apply it to label simple enough. If you need help lemme know :)

EDIT:

As you have specified that you want to leave the gap at the beginning of each line in label you can achieve it by using Edge insets

- (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 0}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. How can I add a gap in the beginning of every line? Is it possible to have label length as long as text length (only for last line)?
You can add gap in the beginning of every line by using Content insets buddy :) for that to work you will have to create a subclass of your label override draw() method create a content inset and set it using labels one of the init :) about having varying length yes you can if you are ready to create subclass of NSTextStorage class and override its various methods (little complicated but do able :))
@samiul-islam-sami : Updated my code to leave gap at the beginning of each line in label have a look :)
@samiul-islam-sami : Please consider accepting the answer if it helped
It shows error with No visible @interface for 'UIViewController' declares the selector 'drawTextInRect:'. My super class in UIViewController.
|
3

You must have forgotten to increase the label's height. The code below is for allowing the UILabel to have multiple lines:

label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; 

Then you have to make sure the UILabel's frame has enough height to show the lines. You can achieve this by calculating the required height for the given text (NSString):

NSString *text = @"YourText"; CGFloat your70Width; // whatever your width is CGSize constraintSize = CGSizeMake(your70Width, MAXFLOAT); UIFont *yourLabelFont; // whatever your font is CGRect requiredFrame = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:yourLabelFont} context:nil]; // Keeps the old x,y coordinates and replaces only the width and height. CGRect oldLabelFrame = label.frame; label.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y, requiredFrame.size.width, requiredFrame.size.height); 

Now the label will be shown nicely in multiple lines.

1 Comment

This worked nicely for me but not until I added [label sizeToFit]; Thanks.
2

To increase the height of the label according to the content if you are using storyboard. Give the label FOUR constraints (Top, Bottom, Leading, Trailing) then go to Attribute Inspector and make lines to 0 and in line break do it WORD WRAP.enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.