I have a basic UILabel that I use in a UITableViewCell
. I'm experiencing some strange behaviour, on some cells (not all) the UILabel gets a grey top border, see picture below. And I'm not sure how to fix it.
I create my UILabel like this:
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];
}
Any ideas on how to fix this?
Update
My cellForRowAtIndexPath
looks like this:
- (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;
}
And the relevant par of configureCell
like this:
- (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;
[...]
}
I had the same problem and solved it by rounding up the height:
newFrame.size.height = ceilf(expectedLabelSize.height);
Got the same issue
Solved by assigning border color and width to UILabel
nameLabel.layer.borderColor = [[UIColor whiteColor]CGColor];
nameLabel.layer.borderWidth = 2.0f;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With