UILabel Background Color Leaks to Border

前端 未结 6 1922
予麋鹿
予麋鹿 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: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;
    }
    

提交回复
热议问题