2

我正在开发一个项目来动态地在 UILabel Width 和 Height 上制作 NSString。我试过:

NSString *text = [messageInfo objectForKey:@"compiled"];
writerNameLabel.numberOfLines = 0;
writerNameLabel.textAlignment = UITextAlignmentRight;
writerNameLabel.backgroundColor = [UIColor clearColor];
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] 
               constrainedToSize:constraint 
                   lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"sizewidth = %f, sizeheight = %f", size.width, size.height);
NSLog(@"writerNameLabel.frame.size.width 1 -> %f",writerNameLabel.frame.size.width);
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];

CGRect labelFram = writerNameLabel.frame;
labelFram.origin.x = cell.frame.size.width - writerNameLabel.frame.size.width - 80;
writerNameLabel.frame = labelFram;
NSLog(@"writerNameLabel.frame.size.width 2-> %f",writerNameLabel.frame.size.width);

在此处输入图像描述

请看绿色气泡而不是灰色气泡。还是不对。

气泡代码

bubbleImageView.frame = CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, writerNameLabel.frame.size.width+15, writerNameLabel.frame.size.height+5);

请指教!谢谢!

4

1 回答 1

1

那是因为你没有重用表格单元格,结构应该是这样的:

NSString *text = [messageInfo objectForKey:@"compiled"];
if(cell == nil) 
        { 
     writerNameLabel.numberOfLines = 0;
     writerNameLabel.textAlignment = UITextAlignmentRight;
     writerNameLabel.backgroundColor = [UIColor clearColor];
     [cell addSubview:writerNameLabel];
}
else {
     writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] 
               constrainedToSize:constraint 
                   lineBreakMode:UILineBreakModeWordWrap];
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];

我已经完成并回答了您的一些问题,这是编写 tableview 控制器的正确方法。你的问题将得到解决。

于 2012-08-20T06:06:48.223 回答