0

Hi I am new to iPhone development. I want to resize my label. I know this question asked many times previously but those answers not working for me. I don't know what I am doing wrong. I did following things : I am using auto layout. I also define number of line to 0. then to resize my label I did following things

NSString *text = @"NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID “;
_sampleText.text = text;
CGRect newSize = _sampleText.frame;
newSize.size.height = 200.0f;
_sampleText.frame = newSize;

I tried [_sampleText sizeToFit]; this also.

But this not working for me. Any one can help me please. Need Help. Thank you.

I tried this one

CGSize maximumSize = CGSizeMake(_sampleText.frame.size.width, 999);
CGSize expectedSize = [text boundingRectWithSize: maximumSize options: NSStringDrawingUsesLineFragmentOrigin
                                            attributes: @{ NSFontAttributeName:[UIFont fontWithName:_sampleText.font.fontName size:_sampleText.font.pointSize] } context: nil].size;
[_sampleText setText:text];
[_sampleText sizeToFit];
CGRect rect = _sampleText.frame;
rect.size.height = expectedSize.height;
_sampleText.frame = rect;

Forgot about text and resizing with text I am not able to resize Uilabel it self

  CGRect rect = CGRectMake(0, 0, 50, 50);
  [_sampleText setFrame:rect];

above code inside viewDidLoad.

So I found some thing. If I disable auto layout option then it works fine. But what if I want to use it with auto layout enable?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • this [link](http://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text) might help you out. Don't forget to vote up. – nikhil84 May 28 '14 at 08:17

2 Answers2

0

Firstly calculate size to fit in UILabel:

NSString *text = @"NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID NILESH KASHID";
CGSize maximumSize = CGSizeMake(_sampleText.frame.size.width, 999);
CGSize expectedSize = [text sizeWithFont:_sampleText.font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
[_sampleText setText:text];
[_sampleText sizeToFit];
CGRect rect = _sampleText.frame;
rect.size.height = expectedSize.height;
_sampleText.frame = rect;
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0
NSString *text = @"";
        CGSize boundingSize = CGSizeMake(300, 1500.0f);
        CGSize requiredSize = [text sizeWithFont:[UIFont systemFontOfSize:12]
                               constrainedToSize:boundingSize
                                   lineBreakMode:NSLineBreakByWordWrapping];
        CGFloat requiredHeight = requiredSize.height;
       int textHeight = requiredHeight+90;
        NSLog(@"%d",textHeight);
        return textHeight; 

Try this...All the best!!!!

madhu
  • 961
  • 9
  • 21
  • `NSLineBreakByWordWrapping` deprecated since IOS7. – nilkash May 28 '14 at 10:48
  • instead u can use CGSize requiredSize = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:boundingSize];text.lineBreakMode = UILineBreakModeWordWrap; – madhu May 28 '14 at 10:50
  • If I disable auto layout then it works fine. But what if I want to use it with auto layout ? – nilkash May 28 '14 at 11:26
  • you are restricting the label with constraints to fit in particulr place by using auto layout. – madhu May 28 '14 at 11:59
  • But I am not putting any constraints. – nilkash May 28 '14 at 12:05
  • yes,now you might disabled.If you use constraints,the label will stick to that position. – madhu May 28 '14 at 12:07
  • NSStringDrawingUsesLineFragmentOrigin attributes: @{ NSFontAttributeName:[UIFont fontWithName:_sampleText.font.fontName size:_sampleText.font.pointSize] } context: nil].size; – madhu May 28 '14 at 12:08
  • that will be used only in ios7,in ios6 it leads to crash – madhu May 28 '14 at 12:08