我有一个在UITableViewCell
中使用的基本UILabel。我遇到了一些奇怪的行为,在一些单元格(不是所有)上,UILabel的顶部边框是灰色的,见下图。我不知道该怎么解决它。
我这样创建我的UILabel:
if (self.postText == nil) {
CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
self.postText = [[UILabel alloc] initWithFrame:postTextRect];
self.postText.backgroundColor = [UIColor whiteColor];
self.postText.textColor = [UIColor darkGrayColor];
self.postText.userInteractionEnabled = NO;
self.postText.numberOfLines = 0;
[self.containerView addSubview:self.postText];
}
有什么办法解决这个问题吗?
更新
我的cellForRowAtIndexPath
看起来像这样:
- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[...]
static NSString *postCellIdentifier = @"PostCell";
PostCell *cell = [tableView dequeueReusableCellWithIdentifier:postCellIdentifier];
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:postCellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath forPost:cellPost];
return cell;
}
configureCell
的相关par如下:
- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
[...]
cell.username.text = cellUser.username;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = cell.postText.frame;
newFrame.size.height = expectedLabelSize.height;
cell.postText.frame = newFrame;
CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
if (containerRect.size.height < 65) {
containerRect.size.height = 65;
}
cell.containerView.frame = containerRect;
[...]
}
发布于 2014-04-15 20:00:57
我也有同样的问题,并通过四舍五入的高度解决了它:
newFrame.size.height = ceilf(expectedLabelSize.height);
发布于 2014-02-27 20:32:08
得到了同样的问题
通过将边框颜色和宽度分配给UILabel来解决
nameLabel.layer.borderColor = [[UIColor whiteColor]CGColor]; nameLabel.layer.borderWidth = 2.0f;
发布于 2015-03-06 15:46:37
一定是iOS的bug。该错误由标签的浮动高度值触发。因此,解决该问题的简单且无副作用的方法是将浮动高度值转换为整数值:
(int)(label.frame.size.height)
https://stackoverflow.com/questions/21065921
复制相似问题