我有一个 ListView ,有时文本很长。当文本是几段时,它往往会在某些时候被截断。部分奇怪的行为是它并不总是在同一个字符处被截断,而是随机的。
这是我用来填充列表的代码:
// CREATING EACH CELL IN THE LIST
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static const NSInteger kLabelTag = 1;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:@"business"];
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
NSString *first_name = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"first_name")];
//Boolean *is_private = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"is_private")];
// Creating a constraint size for the label you are making
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
// Determining the size that you will need for the label based on the comment
// length and the contraint size
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// Creating the label and initializing it with the frame that you have
// determined by your size calculations above
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, MAX(size.height, 44.0f) + 20.0f)];
// setting up the label properties
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];
if ( is_private == nil )
{
label.text = [first_name stringByAppendingString:[@": " stringByAppendingString:comment]]; // comment;
}
else
if ( [is_private isEqualToString:@"0"])
{
label.text = [first_name stringByAppendingString:[@": " stringByAppendingString:comment]]; // comment;
}
else
{
label.text = comment;
}
// adding the label view to the cell that you have created
[cell.contentView addSubview:label];
// CLOSE THE SPINNER
[spinner stopAnimating];
// return the cell for the table view
return cell;
}
//This method will determine how tall each row needs to be. Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
// Again you are getting the constraints because you are going to us this size
// to constrain the height of the cell just like you determined the size for the label.
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
// Again, determining the size that we will need for the label, because it will drive
// the height of the cell
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// Finally, we can determine the height for the cell!
CGFloat height = MAX(size.height, 44.0f);
// return the height, which is the height of the label plus some extra space to make
// it look good.
return height + (10 * 2);
}
这段代码有什么特别不正确的地方吗?还是问题可能源于其他地方?
谢谢!
最佳答案
当你创建标签时,你给它一个最大高度
MAX(size.height, 44.0f)
这意味着标签永远不会高于 44 点。 如果文本(被包裹以适应标签的宽度)高于标签,则其下方的闲置线条仍将位于标签中,但不可见。
您的选择:
-使标签字体自动调整大小
-改变标签的高度,或者切换到动态高度
每次更新时都会变成这样:
myLabel.text = @"some text";
CGRect rect = myLabel.frame;
rect.size.height = textView.contentSize.height;// Adding.size Since height is not a member of CGRect
textView.frame = rect; //your label is now as high as its contents (the text)
-修剪进入标签的文本,使其永远不会超过标签的高度。
关于ios - 列表项的文本在一定长度后被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587946/