Adding a CGGradient as sublayer to UILabel hides the text of label

前端 未结 5 1131
青春惊慌失措
青春惊慌失措 2020-12-03 17:17

I want to add the gradient as a background to label. I used the following code to acheive that. but the problem is that though the gradient color appears on the label, but t

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 17:36

    One more additional think to correct answer. If you are use autolayouts for place your custom view you can get this problem - http://prntscr.com/5tj7bx

    So your view has a different size then subView - UILabel and subLayer - gradient layer.

    I solve this problem by add one method

    class wResultView: UIView {
    var label = UILabel()
    var gradientLayer = CAGradientLayer()
    
    override func layoutSubviews() {
        gradientLayer.frame = self.bounds
        label.frame = self.bounds
    }
    ......
    

提交回复
热议问题