Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The example you provide relies on private text drawing functions that you don't have access to on the iPhone. <strike>The author provides an example of how to do this using public API in a <a href="http://silverity.livejournal.com/27704.html" rel="nofollow noreferrer">subsequent post</a>.</strike> His later example uses a gradient image for the color of the text. (Unfortunately, it appears his blog has since been removed, but see <a href="https://stackoverflow.com/a/4558480/19679">Bach's answer here</a> for the approach he used.)</p> <p>If you still want to draw the gradient for your text color in code, it can be done by subclassing UILabel and overriding -drawRect: to have code like the following within it:</p> <pre><code>CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); CGContextScaleCTM(context, 1.0f, -1.0f); CGContextSelectFont(context, "Helvetica", 20.0f, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextClip); CGContextSetTextPosition(context, 0.0f, round(20.0f / 4.0f)); CGContextShowText(context, [self.text UTF8String], strlen([self.text UTF8String])); CGContextClip(context); CGGradientRef gradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2; CGFloat locations[2] = { 0.0, 1.0 }; CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0, // Start color 1.0, 1.0, 1.0, 0.1 }; // End color rgbColorspace = CGColorSpaceCreateDeviceRGB(); gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); CGRect currentBounds = self.bounds; CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)); CGContextDrawLinearGradient(context, gradient, topCenter, midCenter, 0); CGGradientRelease(gradient); CGColorSpaceRelease(rgbColorspace); CGContextRestoreGState(context); </code></pre> <p>One shortcoming of this approach is that the Core Graphics functions I use don't handle Unicode text properly.</p> <p>What the code does is it flips the drawing context vertically (the iPhone inverts the normal Quartz coordinate system on for the Y axis), sets the text drawing mode to intersect the drawn text with the clipping path, clips the area to draw to the text, and then draws a gradient. The gradient will only fill the text, not the background.</p> <p>I tried using NSString's -drawAtPoint: method for this, which does support Unicode, but all the characters ran on top of one another when I switched the text mode to kCGTextClip.</p>
    singulars
    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. 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.
    3. VO
      singulars
      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