Objective C - CAGradientLayer covers the text in UILabel?

后端 未结 3 1079
星月不相逢
星月不相逢 2021-02-13 19:06

I am trying to add a gradient layet to my UILabel for some reasons the CAGradientLayer covers my text. Am I doing anything wrong

- (voi         


        
3条回答
  •  鱼传尺愫
    2021-02-13 19:56

    I had the same problem. I made this method to get a reference for the shape layer and or generate it if it wasn't there. This method makes sure to throw that layer in the back so that it doesn't cover the label text layer. Just use this and you should be all good, no extra subclasses required.

    - (CAShapeLayer*) getShapeLayerForObject: (UIView*) object{
        CAShapeLayer *maskLayer;
        int loc = -1;
        for(int x = 0; x < object.layer.sublayers.count; x++){
            if([[object.layer.sublayers objectAtIndex:x] isKindOfClass:[CAShapeLayer class]]){
                loc = x;
                break;
            }
        }
        if(loc > -1){
            maskLayer = (CAShapeLayer*) [object.layer.sublayers objectAtIndex:loc];
        }else{
            maskLayer = [[CAShapeLayer alloc] init];
            [object.layer insertSublayer:maskLayer atIndex:0];
    
        }
        return maskLayer;
    }
    

提交回复
热议问题