How can I remove UILabel's gray border on the

2019-04-18 16:48发布

I have added an UILabel to a custom cell's contentView. I met with a strange side effect. On the right border there is a gray edge. I don't know why. I just added an ordinary UILabel, I have done nothing else.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"value1";
label.textAlignment = UITextAlignmentCenter;
[self.contentView addSubview:label];

- (void)layoutSubviews
{
    UILabel *label = [self.contentView.subviews objectAtIndex:i];
    label.frame = CGRectMake(…);
}

enter image description here

4条回答
劫难
2楼-- · 2019-04-18 17:12

This is still happening on iOS 12.

Swift version of Ad Taylor answer:

myLabel.frame.integral

查看更多
forever°为你锁心
3楼-- · 2019-04-18 17:19

First of all, you need to set:

[label setBackgroundColor:[UIColor clearColor]];

Next, set color that you want:

[label layer] setBackgroundColor:[UIColor redColor].CGColor];
查看更多
做个烂人
4楼-- · 2019-04-18 17:20

I know this is an old question but I have just come across this exact same problem, so I thought I could help someone else looking.

For me the problem was that I had floating numbers in my frames. The following fixed it for me:

CGRectIntegral( myLabel.frame )
查看更多
Juvenile、少年°
5楼-- · 2019-04-18 17:28

I don't know the reason, but the solution is to get rid of that gray edge to set the background color of the cell to clearColor.

label.backgroundColor = [UIColor clearColor];

查看更多
登录 后发表回答