UILabel won't adjust height to fit content - ios

I can't get this dynamic label to resize its height according to the content. It logs the correct info but the label is always the same height. Any ideas?
self.descriptionLabel.text = string;
self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
self.descriptionLabel.numberOfLines=0;
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [string sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
//adjust the label the the new height.
CGRect newFrame = self.descriptionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
NSLog(#"the height %f", newFrame.size.height);
self.descriptionLabel.frame = newFrame;

CGSize constraintSize = CGSizeMake(requiredWidth, MAXFLOAT);
CGSize labelSize = [requiredLableText sizeWithFont:requiredFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
returned size will give u the dynamic height of the UILabel

Related

Why is so much extra space being added to my UILabel when I try to change height to fit text?

I have no idea why the height of my UILabel is expanding to such a great height. It leaves the text of my UILabel in the centre. I don't want all of this extra space...
Unwanted extra space
Here is my code (I set the text before this point):
self.infoDescription.numberOfLines = 0;
[self.infoDescription sizeToFit];
self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
Please help :( I just want the height of the UILabel to fit the text exactly.
First set the frame and then make size to fit of label.
**self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
[self.infoDescription sizeToFit];**
Try this:
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = #"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
(original source)
Please check this answer it works for me and it is exactly what you are looking for.
//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;

UITextView add ...more button at the end of visible text

UITextView add ...more button at the end of visible text like on facebook.
I am sorry if it already mentioned somewhere. Specifically I am interested how to count position of the end of visible text with wrapping words
1 . Check UItextView size base on text and Font:
// return the size for UITextView for both IOS7 and IOS6
-(CGSize) getSizeWithMaxLabelSize:(CGSize) maxLabelSize forTextViewWithText:(NSString *) text
{
CGSize expectedLabelSize;
CGSize maximumLabelSize = maxLabelSize;
if (SYSTEM_VERSION_GREATER_THAN(#"6.2")) {
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] forKey: NSFontAttributeName];
CGRect rect =[text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
expectedLabelSize = rect.size;
expectedLabelSize.height = ceilf(expectedLabelSize.height);
expectedLabelSize.width = ceilf(expectedLabelSize.width);
} else {
expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:EPCommentViewFontSize] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
}
return expectedLabelSize;
}
2 . Compare your UITextView height with expectedLabelSize.height to show ViewMore or not
3 . Change UITextView frame.size.height when touched viewMore
CGRect frame = _textView.frame;
frame.size.height = [self getSizeWithMaxLabelSize:CGSizeMake(_textView.frame.size.height, NSIntegerMax) forTextViewWithText:_textView.text].height;
_textView.frame = frame;

UILabel with dynamic height into UITableviewcell

I am developing an app which required to display UILabel into UITableviewCell. I also need to resize UILabel as per text size.
I am using following code for get contentsize of text size.
CGRect rect = [as boundingRectWithSize:CGSizeMake(220.0, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: font} context:nil];
For update frame of UILabel I use following code.
rect.origin.x = cell.lblDescription.frame.origin.x;
rect.origin.y = cell.lblDescription.frame.origin.y;
rect.size.width = cell.lblDescription.frame.size.width;
[cell.lblDescription setFrame:rect];
It set wrong frame.
Please find attached screenshot.
NSString *text = [NSString stringWithFormat:#"%#",[[arr_cart objectAtIndex:indexPath.row] objectForKey:#"name"]];
UIFont *font = [UIFont fontWithName:#"ArialMT" size:12];
CGSize size = [(text ? text : #"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UILabel *lbl_desc=[[UILabel alloc]init];
lbl_desc.numberOfLines = 0;
lbl_desc.frame=CGRectMake(70,18, size.width, size.height);
lbl_desc.lineBreakMode = NSLineBreakByWordWrapping;
lbl_desc.text = (text ? text : #"");
lbl_desc.font = font;
lbl_desc.backgroundColor=[UIColor clearColor];
lbl_desc.textColor = [UIColor darkTextColor];
[cell.contentView addSubview:lbl_desc];
[lbl_desc release];
You have to check the height of the label in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate Method.
CGSize maximumLabelSize = CGSizeMake(your_label_width, FLT_MAX);
CGSize expectedLabelSize = [label_text sizeWithFont:[UIFont fontWithName:#"your_font" size:your_font_size] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
then set the cell height as expectedLabelSize.height.
also do the same thing in your custom cell.
Use following code
[self setDynamicHeightOfLabel:lblName withLblWidth:97 andFontSize:13];
In above method you just need to pass your UILabel name with specific width and fontSize of label.
-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
CGRect lblFrame = myLabel.frame;
lblFrame.size.height = expecteingmyLabelSize.height;
myLabel.frame = lblFrame;
int addressLine = myLabel.frame.size.height/fontSize;
myLabel.numberOfLines = addressLine;
}
Using above code you can set dynamic height of UILabel.

Manage label text dynamically not work properly

CGFloat constrainedSize = 500.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19]; //or any other font that matches what you will use in the UILabel
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];
lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10,y, 300,textSize.height)];
i have tried this code for manage the dynamic text. but if the data comes larger it will not display the whole text.
You constrain the size to width = 500pt, but your textfield is only 300pt wide.
Edit:
It seems, I wasn't clear. If you calculate the height of the label with sizeWithFont and give as constraint a width of 500pt (constrainedSize) and use the calculated height then on a label with only 300pt width, the calculated height is not correct.
This is how it works for me:
CGFloat constrainedSize = 300.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19]; //or any other font that matches what you will use in the UILabel
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];
UILabel* lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, constrainedSize, textSize.height)];
lblDescription.lineBreakMode = NSLineBreakByWordWrapping;
lblDescription.numberOfLines = 0;
lblDescription.font = myFont;
lblDescription.text = myText;
Again: use the same attributes for the label (font, size, lineBreakMode) as you use for the calculation, otherwise it won't fit.
That's because you are alloc and initing the UILabel, and never reset it's frame for bigger size text.
Simply set the frame of the UILabel after a text size change.
This line sets the size of the label, to textSize.height and it doesn't change.
lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10,y, 300,textSize.height)];
You can call setFrame to programmatically change this.
To display a larger text/Multiline text you should use UITextView instead. It is designed to handle this issue.
hi here is code to set dynamic text in label
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [label.text sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
i hope this is helping to you..

iOS auto adjust label height

I'm using the following code to auto adjust the height of a label in a UITableView. It works the majority of the time, but certain times text is cut off. Is there something wrong with my code, or anything else I need to add?
UILabel *textLabel = ((UILabel *)[cell viewWithTag:3]);
textLabel.text = text;
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [text sizeWithFont:textLabel.font constrainedToSize:maximumLabelSize lineBreakMode:textLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = textLabel.frame;
newFrame.size.height = expectedLabelSize.height;
textLabel.frame = newFrame;
In iOS 7 sizeWithFont: constrainedToSize: lineBreakMode: is deprecated, now you should use:
CGSize maxSize = CGSizeMake(296.f, FLT_MAX);
CGRect labRect = [someText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:textLabel.font} context:nil];
textLabel.frame = CGRectMake(0, 0, maxSize.width, labRect.size.height);
textLabel.text = someText;

Resources