0

I am creating a label programatically in a detail view on table cell click.aI am setting the text accordingly and set the label height for that. This is my code :

NSString *mytitleString = [discussionData valueForKey:@"Name"];
 CGSize size =  [mytitleString sizeWithFont:[UIFont systemFontOfSize:14.0]  constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];
 float lbltitleheight = size.height;
 lbltitle.text = mytitleString;

but the problem is i am not able to set the height of lable if it is multiple line. How do i achieve this?

Purva
  • 1,291
  • 2
  • 15
  • 30
  • ok,you add two statement ...... label_mission.lineBreakMode = UILineBreakModeWordWrap; label_mission.numberOfLines = 0; – hitesh Aug 08 '13 at 10:16

9 Answers9

2

You could also use

label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = @"some long text";
[label sizeToFit];
NSLog(@"Label's frame is: %@", NSStringFromCGRect(label.frame));

Where the size will be adjusted accordingly.

Or you simply have to increase the height of your UILabel Dynamically. The sizeWithFont constrainedToSize:lineBreakMode: is the method to use.

//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
IronManGill
  • 7,222
  • 2
  • 31
  • 52
2

Why you can't set labels size? Did you try to set label as multiline:

NSString *mytitleString = [discussionData valueForKey:@"Name"];
CGSize size =  [mytitleString sizeWithFont:[UIFont systemFontOfSize:14.0]  
                         constrainedToSize:CGSizeMake(500, CGFLOAT_MAX) 
                             lineBreakMode:lbltitle.lineBreakMode];
lbltitle.frame = (CGRect){.oririn = lbltitle.frame.origin, .size = size};
lbltitle.numberOfLines = 0;
lbltitle.text = mytitleString;

I also recommend not use method sizeToFit, as when you will call this multiple times - label may become too narrow.

Mikhail
  • 4,271
  • 3
  • 27
  • 39
2

please try this:

CGFloat height=[mytitleString sizeWithFont:[UIFont fontWithName:@"Arial" size:14] constrainedToSize:CGSizeMake(500, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap].height;

hope this will help you.

hpp
  • 619
  • 3
  • 13
1

Your code is correct but you need to set numberOfLines is 0. Because it is 1 bydefault.

lbltitle.numberOfLines = 0;
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1

You can try this

 lbltitle.text = mytitleString;
 lbltitle.numberOfLines=0;
    [lbltitle sizeToFit];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
1

try this:

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 34.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 10.0;
[label setTextColor:[UIColor blackColor]];
[label setFont:[UIFont fontWithName:@"YourFont" size:15]];
[label setTextAlignment:UITextAlignmentCenter];
[label setText:yourText];
Ilario
  • 5,979
  • 2
  • 32
  • 46
1
CGRect rect_mission = CGRectMake(0, stringSize.height + stringSize_award.height+ 190, 280, stringSize_mission.height+60);
hitesh
  • 374
  • 1
  • 7
1

Your code should work correctly. Try changing the numberOfLines for the label and set it to a max number which it can be extended.

Shahin
  • 865
  • 6
  • 21
1
    CGFloat height=[yourlable sizeWithFont:[UIFont fontWithName:@"Arial" size:31] constrainedToSize:CGSizeMake(yourlable.frame.size.width,1000) lineBreakMode:UILineBreakModeWordWrap].height; 

and you get the dynamic height of label.

PeterParker
  • 308
  • 4
  • 8