Anders Anders - 1 month ago 10
Objective-C Question

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

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;
[...]
}

Answer

Decided to use a UITextView instead.

The problem seems to occur when changing the UILabel's frame.

Chaning this code:

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;

to...

[cell.postText sizeToFit];

Did remove the border. But the content wasn't displayed just as I wanted it, that's why I changed to UITextView