title: Apply gradient to label in UICollectionViewCell
body: <p>I am trying to apply the gradient to a UILabel in one of the cells in my UICollectionView. </p>
  
  <p>Following some responses online I added the extension to UIImage: </p>
  
  <pre><code>extension UIImage {
      static func gradientImageWithBounds(bounds: CGRect, colors: [CGColor]) -&gt; UIImage {
          let gradientLayer = CAGradientLayer()
          gradientLayer.frame = bounds
          gradientLayer.colors = colors
          gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
          gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
  
          UIGraphicsBeginImageContext(gradientLayer.bounds.size)
          if let context = UIGraphicsGetCurrentContext() {
              gradientLayer.render(in: context )
              let image = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
              return image!
  
          }
          return UIImage()
      }
  }
  </code></pre>
  
  <p>and when I apply it to a normal label it works like a charm: </p>
  
  <pre><code>let gradientImage = UIImage.gradientImageWithBounds(bounds: someLabel.bounds, colors: [UIColor.black.cgColor, UIColor.white.cgColor])
  someLabel.textColor = UIColor.init(patternImage: gradientImage)
  </code></pre>
  
  <p>The problem is, the label I want to apply the gradient to is a part of a UICollectionViewCell and when I try to do it: </p>
  
  <pre><code>override init(frame: CGRect) {
          super.init(frame: frame)
  
          addSubview(nameLabel)
          nameLabel.snp.makeConstraints { (make) in
              make.top.bottom.trailing.equalToSuperview()
              make.leading.equalToSuperview().inset(10)
          }
  
          let gradientImage = UIImage.gradientImageWithBounds(bounds: nameLabel.bounds, colors: [UIColor.black.cgColor, UIColor.white.cgColor])
          nameLabel.textColor = UIColor.init(patternImage: gradientImage)
      }
  </code></pre>
  
  <p>it doesn't work. 
  When I stopped execution right before creating the gradientImage unfortunately nameLabel.bounds is equal 0:</p>
  
  <pre><code>(lldb) po nameLabel.bounds
  ▿ (0.0, 0.0, 0.0, 0.0)
    ▿ origin : (0.0, 0.0)
      - x : 0.0
      - y : 0.0
    ▿ size : (0.0, 0.0)
      - width : 0.0
      - height : 0.0
  </code></pre>
  
  <p>Is there a way to get the bounds for the label within the UICollectionViewCell definition? Does it depend on how I define the UICollectionView? What am I missing? </p>
  
  <p>Alternatively, do you know any other way to have a label inside UICollectionViewCell with a gradient? </p>
  
  <p>I tried setting the size in: </p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  return CGSize(width: 100, height: 60)
  }
  </code></pre>
  
  <p>I tried changing the definition of layout from automatic to </p>
  
  <pre><code>layout.estimatedItemSize = CGSize(width: 100, height: 60)
  </code></pre>
  
  <p>Nothing seems to help. </p>