Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel have a weird grey top line/border in iOS7, how can I remove it?

Tags:

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.

enter image description here

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;
    [...] 
}
like image 659
Anders Avatar asked Jan 11 '14 18:01

Anders


2 Answers

I had the same problem and solved it by rounding up the height:

newFrame.size.height = ceilf(expectedLabelSize.height);
like image 104
Blago Avatar answered Sep 20 '22 19:09

Blago


Got the same issue

Solved by assigning border color and width to UILabel

nameLabel.layer.borderColor = [[UIColor whiteColor]CGColor]; nameLabel.layer.borderWidth = 2.0f;

like image 32
Konda Avatar answered Sep 19 '22 19:09

Konda