文本在删除幻灯片上延伸到单元格边界之外 [英] Text stretches outside cell boundaries on delete slide
问题描述
UITableViewCellStyleSubtitle的tableView单元将截断文本。
另外,如果使用了删除幻灯片,则多行的常规UITableCell会截断文本。
A tableView cell of UITableViewCellStyleSubtitle will truncate text. Also, a regular UITableCell of multiple lines will truncate text if the delete slide is used.
但是,我的多行UITableViewCellStyleSubtitle单元不会截断插入删除幻灯片时显示文本。相反,它延伸到单元格的边界之外。我可以解决这个问题吗?这是我的一些代码...
However, my UITableViewCellStyleSubtitle cell of mulitple lines doesn't truncate the text when delete slide comes in. Instead, it stretches outside the boundaries of the cell. Can I fix this? Here is some of my code ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
if (cell==nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellType"] autorelease];
// set the labels to the appropriate text for this row
cell.textLabel.text = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupName];
cell.textLabel.lineBreakMode=UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 0;
if ([(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]isDynamic]){
cell.detailTextLabel.text = NSLocalizedString(@"dynamic", @"dynamic");
}
else {
//get and set the group size
int groupSize = [(Group*)[composeData objectInChosenGroupsListAtIndex:indexPath.row]groupSize];
if (groupSize == 1)
cell.detailTextLabel.text = NSLocalizedString(@"1Contact", @"1 contact");
else
cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%dContacts", @"%d contacts"), groupSize];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[self tableView: tableView cellForRowAtIndexPath: indexPath];
CGRect frame = [UIScreen mainScreen].bounds;
CGFloat width = frame.size.width;
int section = indexPath.section;
NSString *title_string = cell.textLabel.text;
NSString *detail_string = cell.detailTextLabel.text;
CGSize title_size = {0, 0};
CGSize detail_size = {0, 0};
if (title_string && [title_string isEqualToString:@""] == NO ) {
title_size = [title_string sizeWithFont:[UIFont systemFontOfSize:22.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:cell.textLabel.lineBreakMode];
}
if (detail_string && [title_string isEqualToString:@""] == NO ) {
detail_size = [detail_string sizeWithFont:[UIFont systemFontOfSize:18.0]
constrainedToSize:CGSizeMake(width, 4000)
lineBreakMode:cell.detailTextLabel.lineBreakMode];
}
CGFloat title_height = title_size.height;
CGFloat detail_height = detail_size.height;
CGFloat content_size = title_height + detail_height;
CGFloat height;
switch ( section ) {
case 0:
height = content_size;
break;
//Just in case
default:
height = 44.0;
break;
}
return height;
}
推荐答案
很难解决!问题在于,如果numberOfLines设置为0,它将始终拉伸文本。因此,我需要计算numberOfLines,并为每个单元格设置它。
This was so tough to work out! The problem is that it will always stretch the text if numberOfLines is set to 0. Therefore, I needed to calculate the numberOfLines, and set it for each cell.
cell.textLabel.numberOfLines = textLabelheight/textLabelFontSize;
这篇关于文本在删除幻灯片上延伸到单元格边界之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!