0

i am fetching the content of my uilabel from the web service so i want to make the uilabel to be dynamic. I want the uilabel to adjust its width depending on how long its content.

the code i have only reduced the size of the text.

lblWinDesc = [[UILabel alloc] initWithFrame:CGRectMake(winDesString.frame.origin.x, winDesString.frame.origin.y + winDesString.frame.size.height, topLine.frame.size.width, lblOwnerName.frame.size.height)];
lblWinDesc.adjustsFontSizeToFitWidth = YES;
lblWinDesc.numberOfLines =0;
lblWinDesc.layer.borderColor = [UIColor whiteColor].CGColor;
[lblWinDesc setFont:[UIFont boldSystemFontOfSize:20]];
[lblWinDesc setTextColor:[UIColor whiteColor]];
[contentScrollView addSubview:lblWinDesc];

i want the label to increase its height when the character/word reaches the maximum width of the uilabel

Aldrin Equila
  • 165
  • 2
  • 17

4 Answers4

0

Well you can try this..

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};
 // NSString class method: boundingRectWithSize:options:attributes:context is
 // available only on ios7.0 sdk.
CGRect rect = [myString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:attributes
                                      context:nil];
//myString is the string you are getting after webservice

Now you can get the height of the label you want to create dynamically by using

rect.size.height;

OR You can use the rect as the frame of your Label. [EDIT] Check the height of the rect, make a UILabel and set the height of the rect as the label's

CGFloat heightOfExpectedLabel = rect.size.height;
UILabel myLabel = [UILabel alloc]initWithFrame:CGRectMake(x,y,width,heightOfExpectedLabel);
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0
lblWinDesc = [[UILabel alloc] initWithFrame:CGRectMake(winDesString.frame.origin.x, winDesString.frame.origin.y + winDesString.frame.size.height, topLine.frame.size.width, lblOwnerName.frame.size.height)];
  lblWinDesc.numberOfLines =0;
lblWinDesc.layer.borderColor = [UIColor whiteColor].CGColor;
[lblWinDesc setFont:[UIFont boldSystemFontOfSize:20]];
[lblWinDesc setTextColor:[UIColor whiteColor]];
   CGSize maximumLabelSizeValue = CGSizeMake(lblWinDesc.frame.size.width, FLT_MAX);
CGSize expectedSubmittedByValueLabelSize = [_lblWinDesc.text sizeWithFont:_lblWinDesc.font constrainedToSize:maximumLabelSizeValue lineBreakMode:_lblWinDesc.lineBreakMode];
CGRect newSubmittedByValueFrame = _lblWinDesc.frame;
newSubmittedByValueFrame.origin.y=_lblWinDesc.frame.origin.y-2;
newSubmittedByValueFrame.size.height = expectedSubmittedByValueLabelSize.height+5;
[_lblWinDesc setFrame:newSubmittedByValueFrame];
[contentScrollView addSubview:lblWinDesc];
Kittu
  • 1,577
  • 1
  • 9
  • 12
  • the label is not displaying. I'm accessing the label from my uiview, and I'm putting the code on the uiview, then the setting of the text is in the controller – Aldrin Equila Oct 15 '14 at 07:09
  • i tried to put a border to see if the label is there, but its not. it disappears – Aldrin Equila Oct 15 '14 at 07:10
  • you are adding Uilabel to contentScrollView. But you are saying I'm accessing the label from my uiview,.so try to get Uilabel from contentScrollView insted of UIview – Kittu Oct 15 '14 at 07:14
  • `-sizeWithFont` is a deprecated method, You have to use `-boundingRectWithSize` for that. – Kampai Oct 15 '14 at 07:19
0

To dynamically increase UILable height you have to calculate number of lines also:

CGRect lblRect = self.label.frame;
CGSize maxSize = CGSizeMake(self.label.frame.size.width, MAXFLOAT);

CGRect labelRect = [self.label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];

CGFloat labelHeight = labelRect.size.height;
int lines = labelHeight / 16; // This is fix height (default height) of your label
[self.label setNumberOfLines:lines];

lblRect.size.height = labelHeight;
[self.label setFrame:lblRect];
Kampai
  • 22,848
  • 21
  • 95
  • 95
0

i was able to fixed it by putting the code on my controller where i set the text. the label disappears because there is no text yet.

i simply used:

[myLabel sizeToFit];
Aldrin Equila
  • 165
  • 2
  • 17