UILabel Background Color Leaks to Border

前端 未结 6 1940
予麋鹿
予麋鹿 2021-02-19 03:48

I\'m creating a UILabel to which I set the background color and corner radius with the following code:

self.scoreLabel.backgroundColor = [UIColor DISRed];// cust         


        
相关标签:
6条回答
  • 2021-02-19 04:01

    I ran into the same problem with the UIButton's background color leaking around the edge of its border.

    Instead of setting the UIButton background color on the UIButton, set it on the UIButton's layer.

    Replace:

    self.scoreLabel.backgroundColor = [UIColor DISRed];// custom red`
    

    With this:

    self.scoreLabel.layer.backgroundColor = [[UIColor DISRed] CGColor];// custom red`
    
    0 讨论(0)
  • 2021-02-19 04:06

    Well, a lot of answers...

    I found the problem persists, as long as the UIViews background color is used and not the background color of the UIViews 'layer'. In addition, of course, masking needs to be enabled.

    For a UICollectionViewCell subclass the code is:

    - (void)prepare
    {
        self.contentView.layer.backgroundColor = UIColor.redColor.CGColor;
        self.contentView.layer.cornerRadius = 25.0;
        self.contentView.layer.borderColor = UIColor.blackColor.CGColor;
        self.contentView.layer.borderWidth = 1.0;
        //Enable to optimize image views: self.contentView.layer.shouldRasterize = YES;
        //Enable to optimize image views: self.contentView.layer.rasterizationScale = UIScreen.mainScreen.scale;
        self.contentView.layer.masksToBounds = YES;
        self.contentView.clipsToBounds = YES;
    }
    

    To make the setting of the background color more comfortable and less error prone, some could add this:

    /*
     setBackgroundColor:
    
     */
    - (void)setBackgroundColor:(UIColor *)p_BackgroundColor
    {
        super.backgroundColor = nil;
        self.contentView.layer.backgroundColor = p_BackgroundColor.CGColor;
    
        self.contentView.layer.masksToBounds = YES;
        self.contentView.clipsToBounds = YES;
    }
    
    0 讨论(0)
  • 2021-02-19 04:13

    For those who are still facing the issue of border color leaking out: Go through the below code, please note you will need to set frames & border width as per your requirement, I'm setting the position as view's center

     let badgeSize: CGFloat = 10
     let redBadge = UIView(frame: CGRect(x: view.center.x, y:view.center.y, width: badgeSize, height: badgeSize))
     redBadge.layer.borderColor = UIColor.white.cgColor
     redBadge.layer.borderWidth = 2
     redBadge.backgroundColor = .red
     redBadge.layer.cornerRadius = badgeSize * 0.5
     redBadge.clipsToBounds = true
     redBadge.layer.masksToBounds = true
     redBadge.maskLayerOnView(radius: badgeSize * 0.5)
     view.addSubview(redBadge)
    

    Secondly, we need to write an extension on UIView

    extension UIView{
     func maskLayerOnView(radius: CGFloat){
       let maskLayer = CAShapeLayer()
       maskLayer.path = UIBezierPath(roundedRect: self.bounds,
                        byRoundingCorners: [.allCorners], 
                        cornerRadii: CGSize(width: radius, 
                        height: radius)).cgPath
       self.layer.mask = maskLayer
     }
    }
    

    This code snippet removes the border color separating out, one can replicate this behaviour on any kind of views.

    For detailed explanation please see this article.

    0 讨论(0)
  • 2021-02-19 04:14

    It is probably anti aliasing issue. you can better fix it by adding a bezier path around the corners.

    CAShapeLayer *subLayer = [[CAShapeLayer alloc] init];
    [subLayer setFillColor:[UIColor clearColor].CGColor];
    [subLayer setStrokeColor:[UIColor whiteColor].CGColor];
    [subLayer setLineWidth:1.0];
    [subLayer setPath:[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.layer.cornerRadius].CGPath];
    [imageView.layer addSublayer:subLayer];
    
    0 讨论(0)
  • 2021-02-19 04:21

    I created my own UILabel and background colour does not seem to be leaking.

    1. Write this in .h file of your project.

      UILabel *label;

    2. Write this in .m file of your project.

    label=[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];//Set frame of label in your viewcontroller.
        [label setBackgroundColor:[UIColor redColor]];//Set background color of label.
        [label setText:@"Label"];//Set text in label.
        [label setTextColor:[UIColor blackColor]];//Set text color in label.
        [label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
        [label.layer setCornerRadius:50.0];//Set corner radius of label to change the shape.
        [label.layer setBorderWidth:8.0f];//Set border width of label.
        [label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
        [label.layer setBorderColor:[UIColor greenColor].CGColor];//Set Border color.
        [self.view addSubview:label];//Add it to the view of your choice.
    
    0 讨论(0)
  • 2021-02-19 04:22

    I was also facing the same problem. It was a silly mistake. I always forget to tick clipToBounds in case of cornerRadius.

    So, just ticking the Clip to Bounds for UILabel in Storyboard fixed my problem.

    And yes, we need to keep the below code too:

    label.layer.masksToBounds = true
    
    0 讨论(0)
提交回复
热议问题