55

I'm working on an application, in which I'm required to autoresize the text area on basis of text to be displayed.

Firstly, I'm not sure for this either I should use UILabel (Logically is the best choice for displaying static text, which is in my case) or UITextView.

How I wish to use it?
I want to simply init my Label or text view for that matter with Text. Instead I define the frame first and then restrict my text in that area.

If you can suggest the right solution, that will be a great help.

I went through documentation and other references but didn't find much which could help me here or I could've overlooked it.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Muhammad Uzair Arshad
  • 1,549
  • 1
  • 11
  • 9

10 Answers10

97

The sizeToFit method worked just great.

I did following.

UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(6,3, 262,20 )]; // RectMake(xPos,yPos,Max Width I want, is just a container value);

NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...";

testLabel.text = test;
testLabel.numberOfLines = 0; //will wrap text in new line
[testLabel sizeToFit];

[self.view addSubview:testLabel];
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
Muhammad Uzair Arshad
  • 1,549
  • 1
  • 11
  • 9
29

You can find a text size with :

CGSize textSize = [[myObject getALongText] 
                    sizeWithFont:[UIFont boldSystemFontOfSize:15] 
                    constrainedToSize:CGSizeMake(maxWidth, 2000)
                    lineBreakMode:UILineBreakModeWordWrap];

then you can create your UILabel like that :

UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,textSize.width, textSize.height];
[lbl setNumberOfLines:0];
[lbl setLineBreakMode:UILineBreakModeWordWrap];
[lbl setText:[myObject getALongText]];
j_freyre
  • 4,623
  • 2
  • 30
  • 47
  • 2
    Wrong: you should not ask the NSString what size it will be when you will draw it inside a UILabel. – Berik Mar 13 '13 at 11:17
15

In Swift:

testLabel = UILabel(frame: CGRectMake(6, 3, 262, 20))
testLabel.text = test
testLabel.numberOfLines = 0
testLabel.sizeToFit()

In Objective C

UILabel *testLabel = [[UILabel alloc] initWithFrame: CGRectMake(6, 3, 262, 20)]];
testLabel.text = test;
testLabel.numberOfLines = 0;
[testLabel sizeToFit];
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Segev
  • 1,267
  • 16
  • 21
9

If you want to resize the UILabel only in height, use this:

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

CGRect titleLabelBounds = self.titleLabel.bounds;
titleLabelBounds.size.height = CGFLOAT_MAX;
// Change limitedToNumberOfLines to your preferred limit (0 for no limit)
CGRect minimumTextRect = [self.titleLabel textRectForBounds:titleLabelBounds limitedToNumberOfLines:2];

CGFloat titleLabelHeightDelta = minimumTextRect.size.height - self.titleLabel.frame.size.height;
CGRect titleFrame = self.titleLabel.frame;
titleFrame.size.height += titleLabelHeightDelta;
self.titleLabel.frame = titleFrame;

Now you can use titleLabelHeightDelta to layout other views depending on your label size (without using autolayout).

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Berik
  • 7,816
  • 2
  • 32
  • 40
6

I'm not sure I totally understand the question, but you can use the sizeToFit method on a UILabel (the method is inherited from UIView) to change the size according to the label text.

Erik Tjernlund
  • 1,963
  • 13
  • 12
6

The easiest way to find the no. of lines depending on text. You can use this code:

ceil(([aText sizeWithFont:aFont].width)/self.bounds.size.width-300); 

it returns some float value.

[lbl setNumberOfLines:floatvalue];
Dan J
  • 25,433
  • 17
  • 100
  • 173
Anand
  • 2,086
  • 2
  • 26
  • 44
2

Please note that with autolayout call sizeToFit won't change size, because it will be changed later by autolayout calculations. In this case you need to setup proper height constraint for your UILabel with "height >= xx" value.

Ilja Popov
  • 1,091
  • 8
  • 8
2

Because you're going to use this solution countless times in your app, do the following:

1) Create a directory called extensions and add a new file inside called UILabel.swift with the following code:

import UIKit

extension UILabel {
    func resizeToText() {
        self.numberOfLines = 0
        self.sizeToFit()
    }
}

2) In your app code, define the label width you want and just call resizeToText():

label.frame.size.width = labelWidth
label.resizeToText()

This will maintain width while increasing the height automatically.

Alex
  • 424
  • 4
  • 10
0

try bellow code, it works for multiline

self.labelText.text = string;
self.labelText.lineBreakMode = NSLineBreakByWordWrapping;
self.labelText.numberOfLines = 0;
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
0

You can change the size of label based on length of the string by, using this function

func ChangeSizeOfLabel(text:String) -> CGSize{

    let font = UIFont(name: "HelveticaNeue", size: 12)!
    let textAttributes = [NSFontAttributeName: font]
    let size = text.boundingRectWithSize(CGSizeMake(310, 999), options: .UsesLineFragmentOrigin, attributes: textAttributes, context: nil)
    let adjustSize = CGSizeMake(size.width, size.height)
    return adjustSize
}

and use it like this :

let

showLabel.frame = CGRectMake(x, y, width , self.ChangeSizeOfLabel("Hello , Height is changing dynamically.....").height)
VVN
  • 1,607
  • 2
  • 16
  • 25