How to fit a text with various length in a UITableViewCell? - objective-c

What I have is:
a NSString which can have any length between 1 and 400 characters
a UITableViewCell (custom layout)
I tried using an UILabel with multiple lines, set the text, and call sizeToFit. That doesn't work always, most of the time the UILabel just clips off the part of the string that doesn't fit. Also, due the varying length of the text I'd need differently sized UITableViewCells, and at the time "tableView: cellForRowAtIndexPath:" is called I don't know what the height will be.
So what I need is a non-scrolling UI element which is able to display text and resizes its height (the width should remain constant) to exactly fit the text. As mentioned the sizeToFit method produces mostly garbage.

You can use SizeWithFont: to calculate the desired height for your cell and store it in an Array so that you can return that height in HeightForRowAtIndexPath. If you need to update the text, just have a method that re-calculates the height, saves it to the array, and updates the table. Something like:
CGSize constraintSize;
constraintSize.width = 290.0f;
constraintSize.height = MAXFLOAT;
NSString *text = #"YOUR TEXT"
CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"height: %f",theSize.height);
will give you the height.

This configuration should give you something simillar to what you see when you enter a loooong number in the phone app -
label.minimumFontSize = 4; //a very small font size
label.adjustsFontSizeToFitWidth = YES;
label.lineBreakMode = UILineBreakModeWordWrap;// change to what works for you
label.numberOfLines = 0;
See lineBreakMode Documentation

Related

Calculate UILabel height gives wrong values

I am trying to "forecast" what the size of a label is going to be (width is known, only height).
I am trying to use this:
CGSize possibleSize = [text sizeWithFont:[UIFont fontWithName:#"American Typewriter" size:16]
constrainedToSize:CGSizeMake(self.collectionView.frame.size.width ,9999)
lineBreakMode:NSLineBreakByWordWrapping];
Which gives very inaccurate results (even changing the fonts and the size won't make it better, for example I get the height 30 instead of 80).
I have read that other people also don't get good results with it. Am I using it right?
I have also tried:
UILabel *test=[[UILabel alloc] initWithFrame:self.collectionView.frame];
test.text=[dic objectForKey:#"text"];
test.font=[UIFont fontWithName:#"American Typewriter" size:12];
[test sizeToFit];
NSLog(#"%f",test.frame.size.height);
I have to know what the height is going to be, and this method is not even close.
Is there some other way that gives reasonable results?
This sizeWithFont method is now deprecated, this new method works best
NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(self.label.frame.size.width, 9999);
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:#"American Typewriter" size:16] forKey: NSFontAttributeName];
CGSize expectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
CGFloat labelHeight = expectedLabelSize.height;
where labelHeight is the height that the label will be calculated from the amount of text due to load into the label.
I hope this helps, cheers, Jim.
#matt you are onto something, but I'll add that you should set the number of lines on that label to 0, before you calculate sizeWithFont.
You may also try to replace
CGSizeMake(self.collectionView.frame.size.width ,9999)
with
CGSizeMake(self.collectionView.bounds.size.width ,FLT_MAX)
the key element being "bounds" instead of frame.
Lastly, ensure you're not getting nothing for [dic objectForKey:#"text"].
Use
NSAssert(dic[#"text"]);
if ([dic[#"text"] isEqualToString:""]) {
; //empty string
}
First of all you need to allow a UILabel to be multiline, by default it is one line (set number of line to 0)
yourLabel.numberOfLines = 0; // means - label can be multiline
Secondly, it looks like you are calculating size for a bigger font that the label has in fact. Consider using the same size so set the calculations correctly.
Moreover if you support only iOS 7 and newer consider usage of sizeWithAttributes method introduced in iOS 7 or boundingRectWithSize:options:attributes:context - replacement for the method you use from iOS 7 and further for text size calculations.
Finally (advice) if you need that height value only for setting height of the label maybe you should consider using auto layout (it will be much easier to deal with).

How to find the position of the last text line in a multiline UILabel or otherwise have UILabel have 0 padding

I have a UILabel that has both -numberOfLines set to 3 and text-size auto shrink and I need to align another UIView to this UILabel's last line of text. That is, I might need to align to the y position of line 0, 1 or 2, depending on the text inside the label (and the distance between these lines of text may vary depending on whether the text is long enough that it triggered font resizing).
But:
UILabel doesn't expose a contentSize
the label's bounds extend past the last line of text (there seems to be a content inset), so aligning to the bounds won't work.
subclassing UILabel and doing something like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0., 0., -30., 0.};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
just happens to work for the case where I have 3 lines and the font size was auto shrunk, but I still can'r figure out a generic way of subtracting insets for the general case, regardless of text size. And I don't seem to be able to use -boundingRectWithSize:options:context: either: it either returns a single line equivalent rect or, If I play around with the options, a a rect the same size of the original label (that is, including the extra insets I'm trying to get rid of). Mind you, the idea behind removing any insets is that if I have no way of knowing where the last line of text is, at least I can remove any insets in the label so that the last line of text aligns with the label's bounds.origin.y + bounds.size.height.
Any thoughts?
I don't know if the problem was that originally I was using boundingRectWithSize on non-attributed text or what but now this seems to work:
NSString *text = <get text from CoreData>;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: self.titleLabel.font}];
CGRect rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
if (!rect.size.height || rect.size.height > self.titleLabel.frame.size.height) {
attributedText = [[NSAttributedString alloc] initWithString:text attributes:#{NSFontAttributeName: [UIFont boldSystemFontOfSize:self.titleLabel.font.pointSize * self.titleLabel.minimumScaleFactor]}];
rect = [attributedText boundingRectWithSize:self.titleLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
}
self.titleLabel.frame = rect;
self.titleLabel.attributedText = attributedText;
While this doesn't really find the position of the bottom of the last line of text in the UILabel (the label still adds some padding at the bottom... not sure if to account for descenders), it adjusts the label's bounds close enough to the bottom that I can at least align based on bounds.origin.y + bounds.size.height and it looks good enough.

Calculate Cell Height without Using Fixed Values

I need to calculate the height for a UITableViewCell with includes multiple rows of wrapping text. So far I couldn't find a 100% exact way to do this. Furthermore the technique I currently use relies heavily on fixed values:
NSString *cellText;
cellText = #"Very long multi line text in this String ...";
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 32;
Fixed values used in just in these few lines:
Fontsize (14)
Font Type (Helvetica)
CGSize, Width (280.0f)
Random padding value that I found is needed (32)
Is there a nice way to rewrite this in a way that all these fixed values are fetched and calculated dynamically?
That would help a lot to make the layout more responsive and the code reusable going forward. I appreciate any best practices.
Your code seems good but u missed is cell appropriate calculation.U need to add label's topmost postion and label's bottommost postion in cell(parentView).
Just calculate like this
return labelSize.height + 2*yourLabelInCell.frame.origin.y //here label's topmost postion and label's bottommost postion in cell added
Label will adjust appropriately in cell.

How to make UIView like UILabel to scretch and shrink depending on the amount of texts or content in it?

If we put too much text, UILabel would shrink the texts.
Sometimes we put 4 lines in a text. Sometimes there are 20 lines in a text. Sometimes there are none.
Also text may be short or long.
How to make UILabel to fit the amount of that texts?
Also how to to make UIView that encompass that UILabel to also fit the larger or smaller UILabel?
Use the UIKit Additions to NSString to determine the size of a text with a given font.
NSString *myLongText = #"......" // some long text.
UIFont *font = [UIFont systemFontOfSize:12];
CGSize size = [myLongText sizeWithFont:font
forWidth:maxWidth
lineBreakMode:NSLineBreakByWordWrapping];
myLabel.frame = (CGRect){myLabel.frame.origin, size};

UITextView size for UITableViewCell

I have a tableview, one of the rows contains a cell that contains a UITextView. I need to know the size of the textview because I need it to fit the cell and return that size in 'heightForRowAtIndexPath' method. Using the NSString method for size only works for labels, not for textviews. What are my options?
Thanks
Does the UITextView already have the proper size, or do you need to resize it as well to exactly contain its text? There are no out-of-the-box methods to do that I believe.
If it already has the proper size, you can just get the height from its frame:
CGRect frame = textView.frame;
CGSize size = frame.size;
CGFloat = size.height;