0

I'm new in iOS and facing issue regarding to set the width and height of UILabel. There are many answer on this question but nothing help me.

enter image description here

As in the image both label get overlap. I need that the below label according to upper label text. How to do that I also try AutoLayout and I 'm using code like this

I need to set the UILabel Height Auto according to text. like this

enter image description here

enter image description here

[lblSiteName setNumberOfLines:0];
[lblSiteName sizeToFit];

I'm using .XIB .How to do this. Thanks in Advance!

Muju
  • 884
  • 20
  • 54
  • see this http://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text – Anbu.Karthik Feb 15 '17 at 07:15
  • give your label hight constrain greater then equal and set number of line 0 and write one method that find your text hight and set it to your label – Himanshu Moradiya Feb 15 '17 at 07:15
  • -(CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font { CGSize size = CGSizeZero; if (text) { CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil]; size = CGSizeMake(frame.size.width, ceil(frame.size.height)); } return size; } – Himanshu Moradiya Feb 15 '17 at 07:18
  • @Muju, What do you exactly want? Your second label will just stay below your first label's text with some padding? or aligned with `clientname`? – iPeter Feb 15 '17 at 07:51
  • Possible duplicate of [Adjust UILabel height depending on the text](http://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text) – Jigar Feb 15 '17 at 07:51
  • @iPeter No Padding. Client Label should be swap down according to text of Site Name Label.That I want. – Muju Feb 15 '17 at 08:46
  • I recommend to use AutoLayout. Both links above are old and no longer applicable since the accepted answer gives the wrong solution. – koen Feb 15 '17 at 14:02
  • You can make outlets of Height and width constraint of UILabel and set them dynamically or you can set width and height constraint less than or greater than of particular size. – Gurjinder Singh May 17 '17 at 06:58

1 Answers1

0

Try to use this:

+ (CGSize) labelSize: (UILabel *) label
{
    CGSize maximumLabelSize = CGSizeMake(label.frame.size.width, FLT_MAX);
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];
    return labelSize;
}

By this code you can get the fix size of the String for given UIFont.

Sumeet Mourya
  • 434
  • 1
  • 7
  • 23
  • How to call this method. – Muju Feb 15 '17 at 09:11
  • `UIlabel *label = [UIlabel alloc]initwith: Frame];` `label.font = yourFont;` `CGSize labelSize = [self labelSize:label];` `label.frame = CGRectMake(0.0, 0.0, labelSize.width, labelSize.height);` – Sumeet Mourya Feb 15 '17 at 09:12
  • `sizeWithFont` has been deprecated a while now, use `sizeWithAttributes:` instead. – koen Feb 15 '17 at 14:00