Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : How can I set frame of UILabel according to its text?

I want to develop a functionality that to set frame of UILabel according to its text means when text is change its frame is change? If yes, then please share any link or any idea to develop this.

Thanks in advance.

like image 671
Nikunj Jadav Avatar asked Aug 30 '11 10:08

Nikunj Jadav


2 Answers

label.text = @"some text of random length";
[label sizeToFit];

If the text might be more than 1 line, add label.numberOfLines = 0; before calling sizeToFit;

like image 182
Filip Radelic Avatar answered Sep 28 '22 08:09

Filip Radelic


try this

NSString *sample = @"...";

CGSize txtSz = [sample sizeWithFont:[UIFont fontWithName: @"Helvetica" size: 16]];

CGRect lblFrame = CGRectMake(10,50, txtSz.width, txtSz.height);

yourLabel.frame = lblFrame;
like image 35
Karthikeyan Avatar answered Sep 28 '22 08:09

Karthikeyan