Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I figured it out. Basically, we can override <code>drawRectInText</code> and use our own pattern to color the fill. The advantage of doing this is that we can resize the image into our pattern frame.</p> <p>First we create a CGPattern object and define a callback to draw the pattern. We also pass the size of the label as a parameter in the callback. We then use the pattern that is drawn in the callback and set it as the fill color of the text:</p> <pre><code>- (void)drawTextInRect:(CGRect)rect { //set gradient as a pattern fill CGRect info[1] = {rect}; static const CGPatternCallbacks callbacks = {0, &amp;drawImagePattern, NULL}; CGAffineTransform transform = CGAffineTransformMakeScale(1.0, -1.0); CGPatternRef pattern = CGPatternCreate((void *) info, rect, transform, 10.0, rect.size.height, kCGPatternTilingConstantSpacing, true, &amp;callbacks); CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL); CGFloat alpha = 1.0; CGColorRef patternColorRef = CGColorCreateWithPattern(patternSpace, pattern, &amp;alpha); CGColorSpaceRelease(patternSpace); CGPatternRelease(pattern); self.textColor = [UIColor colorWithCGColor:patternColorRef]; self.shadowOffset = CGSizeZero; [super drawTextInRect:rect]; } </code></pre> <p>The callback draws the image into the context. The image is resized as per the frame size that is passed into the callback.</p> <pre><code>void drawImagePattern(void *info, CGContextRef context) { UIImage *image = [UIImage imageNamed:@"FontGradientPink.png"]; CGImageRef imageRef = [image CGImage]; CGRect *rect = info; CGContextDrawImage(context, rect[0], imageRef); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload