Reputation:
I define with a separate UILabel Class the margin from my Label in the Cell:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {5, 10,5, 10};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
With following code, I define the height from the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!self.customCell) {
self.customCell = [self.chatTableView dequeueReusableCellWithIdentifier:@"CellChat"];
}
self.customCell.sender.text = [array objectAtIndex:indexPath.row];
[self.customCell layoutIfNeeded];
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height +1;
}
My question: When I test the application then it will not take the word wrap on the right place. Why? Does it have something to do with the UILabel Class?
EDIT:
Thanks, but I still doesn't work. I don't have an error, but when I am load the table view it does not display text. At the moment, my code looks like that:
Imports:
#import "ChatViewController.h"
#import "ChatTableViewCell.h"
@interface ChatViewController ()
@property (strong, nonatomic) ChatTableViewCell *customCell;
@end
Next:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:@"Arial" size:15];
PFObject *tempObject = [array objectAtIndex:indexPath.row];
gettingSizeLabel.text = [tempObject objectForKey:@"Sender"];
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(140, MAXFLOAT);
CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
return expectedSize.height + (10*2);
}
My cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer = @"CellChat";
ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
PFObject *tempObject = [array objectAtIndex:indexPath.row];
cell.sender.text = [tempObject objectForKey:@"Sender"];
cell.receptor.text = [tempObject objectForKey:@"Receptor"];
cell.sender.lineBreakMode = NSLineBreakByWordWrapping;
cell.sender.numberOfLines = 0;
return cell;
}
Upvotes: 0
Views: 833
Reputation: 4550
Edit
Do not use self.customCell
inside - (CGFloat)tableView:tableView heightForRowAtIndexPath:indexPath
Instead, calculate the cell height something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = font;
gettingSizeLabel.text = [array objectAtIndex:indexPath.row]; // your text inside [array objectAtIndex:indexPath.row];
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(yourMaxWidth, MAXFLOAT);
CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
return expectedSize.height /*then add your margin 'y' margin multiplied by two for the top and bottom margin*/;
}
and dont forget to set
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"CellChat";
self.customCell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!self.customCell)
{
self.customCell = [[YourCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
// dont forget to set this
self.customCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.customCell.textLabel.numberOfLines = 0;
// [self.customCell sizeToFit]; optional depending from what you want
return self.customCell;
}
Hope this have helped you, happy coding.. Cheers! :)
Upvotes: 1