calculation of cell height dynamically - ios

I have a tableview in which there is a uilabel inside the cell. label takes variable height on the basis of text.My codes are below
- (CGFloat)getLabelHeight:(UILabel*)label
{
CGSize constraint = CGSizeMake(label.frame.size.width-10, 1000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [label.text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:label.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height;
}
but setting constraint is difficult one. AnyOne knows the easiest way to calculate the height of cell dynamically and correct way of setting constraint.

If you are using autolayout then it will be easiest.Just add top,left,right and botton constraints for your UILabel, set numberofLines to 0 and line break mode to wordWrap. and in your heightForRow method pass UITableViewAutomaticDimension.
P.S. If there is only label in your cell then instead of using custom cell , use UITableViewCell and use its text label.You then won't have to manage any constraints.

Related

UITableViewAutomaticDimension not setting correct height with emoji characters in the UILabel of UITableviewCell

I am using UITableViewAutomaticDimension for dynamic height of cells of a UITableview. Everything works fine when I set plain text in the label inside the UITableviewCell.
Problem occurs with the cell height when I set plain text along with emoji characters in the label. The cell height do increase dynamically but the height is wrong due to emoji characters. I think UITableview may be considering the unicode as a text instead of emoji icon hence it returns only plain text height.
In my case label's x origin is also dynamic.
Please see the below screen shots for the problem occurring,
Any suggestions?
Remove UITableViewAutomaticDimension, calculate height of label dynamically by below given methods and manage cell height accordingly.
//--- Calculate Height Of String ---//
+(CGFloat)textHeight:(NSString*)text withFont:(UIFont*)font andMaxWidth:(CGFloat)maxWidth
{
CGFloat maxHeight = 99999;
CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);
NSDictionary *attributes = #{NSFontAttributeName: font};
CGRect expectedLabelSize = [text boundingRectWithSize:maximumLabelSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
return expectedLabelSize.size.height;
}

Make UILabel height zero if label empty/storyboard

I have a lot of labels stacked up for a contact info screen. If any of the labels are empty, I would love to zero out their height and have them occupy no space to avoid empty space on the screen. However, I have created the screen in storyboard and the labels have been assigned heights and y values.
Here is code I've been trying to use to alter label height but cannot get it to work. Perhaps it is designed to work only for labels created programmatically and the storyboard settings override what I am doing here.
NSString *string = #"some text";
CGSize maximumLabelSize = CGSizeMake(280,1000);
 
// use font information from the UILabel to calculate the size
CGSize expectedLabelSize = [string sizeWithFont:myLabel.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
 
// create a frame that is filled with the UILabel frame data
CGRect newFrame = myLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = expectedLabelSize.height;
// put calculated frame into UILabel frame
myLabel.frame = newFrame;
Is there a way to get the height of labels created in storyboard to zero out if they are empty?
you can override the following method of the view containing your label and do something like that:
-(void) layoutSubviews {
myLabel.frame = CGRectZero;
}
This method is in order to allow to layout the subviews or you view. Now I don't know If you have used contraints to define the height of your label. If so you should change in the same way the height of your label. Note the in the above example I used CGRectZero.

Dynamic UITableView heightForRowAtIndexPath

I'm facing this problem:
I have a tableView and a customCell, on my custom cell class I set font size and font colors for the labels that I have there, my issue is that in order to calculate the heightForRowAtIndexPath I don't have access to the labels font sizes, what I do in that method create temporary labels with the same text value that I set on the custom cell class and calculate the label height in order to set the heightForRowAtIndexPath height depending on the content but since I don't have the correct font it gives me a different height and I see spaces between each cell, is there any way to solve this?
Why not create global macros for the different font sizes.
#define LABEL1_FONT [UIFont fontWithName:#"Helvetica Neue" size:17]
Then you can make the check for the height:
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font =LABEL1_FONT;
gettingSizeLabel.text = yourText;
CGSize maximumLabelSize = CGSizeMake(291, 9999);
CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
// Get height required for label title
float lineHeightInfo = expectedSize.height / LABEL1_FONT.lineHeight;

UITableViewCell get subview height with auto-layout

I have a UITableViewCell subclass on which I'm using auto-layout, and I want to get the height of a UILabel subview once the auto-layout process has been completed.
I tried layoutSubviews and getting the frame property of my UILabel, but this seemed to be the old height of the label before it was recycled.
I then tried to override layoutIfNeeded but I found it was never called.
How can I get the frames of my UITableViewCell subviews once auto-layout is completed and the constraints have been applied? Surely this should be really simple!
Assuming that the label is an immediate subview of the table cell's content view, then my quess is that you could access the final, on-screen value of the label's bounds in the layoutSubviews method for the content view of the table cell (rather than the layoutSubviews method of the table cell itself).
However, if your label is not multi-lined, then simply grab the label's height from its attributedText property at anytime as follows:
NSAttributedString *attrString = label.attributedText;
CGFloat labelHeight = ceilf(attrString.size.height);
Alternatively, you could use the label's font property to determine the label's height (again, only if the label is not multi-lined):
UIFont *font = label.font;
CGFloat labelHeight = ceilf(font.lineHeight);
If your label is multi-lined, then the best choice for calculating the line height is as follows:
// but you have to know the label's width
CGFloat labelWidth = …
NSAttributedString *attrString = label.attributedText;
CGRect rect = [attrString boundingRectWithSize:CGSizeMake(labelWidth, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGFloat labelHeight = ceilf(rect.size.height);
You would calculate the label's width manually. Often it's the main view's bounds width minus some fixed amount for horizontal spacing margins.

Change label height in tableview in iOS 7

I have a label in custom cell in table view.
I want to change cell height and also label height(at first I am showing only 2 line, I want to show more text) after user taped a cell,
I am able to change cell height based on cell content, but unfortunately I cannot change label height, :((
I've read tons of stackoverflow answers, but still nothing.
it is believed that this would work:
CGSize labelSize = [#"Hello World!" sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(label.frame.size.width,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
but this is deprecated in iOS 7; can any one please help me in resizing my label in cell?
If you want the height of your label relative to the height of your cell and you use Storyboard, you should determine the size of your label with constraints in relation to the ContentView of the cell. Just set the top and bottom distance from your label to the ContentView and no additional height for the label.
If you dont know how to set Storyboard and constraints, this might help: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html
UITableViewCell lays out it's content in -layoutSubviews. Therefore, if you want to simply have a custom layout logic, you need to subclass UITableViewCell and override the -layoutSubviews method. It's a good idea to call super anyway before applying your logic, though.
You may use something like this to get UIlabel size
-(CGFloat)getLabelSize:(UILabel *)label fontSize:(NSInteger)fontSize
{
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:fontSize], NSFontAttributeName,
nil];
CGRect frame = [label.text boundingRectWithSize:CGSizeMake(270, 2000.0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
CGSize size = frame.size;
return size.height;
}

Resources