Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a technique that's similar to Matt Gallagher's, which will generate an inverted text mask with an image.</p> <p>Allocate a (mutable) data buffer. Create a bitmap context with an 8-bit alpha channel. Configure settings for text drawing. Fill the whole buffer in copy mode (default colour assumed to have alpha value of 1). Write the text in clear mode (alpha value of 0). Create an image from the bitmap context. Use the bitmap as a mask to make a new image from the source image. Create a new UIImage and clean up.</p> <p>Every time the textString or sourceImage or size values change, re-generate the final image.</p> <pre><code>CGSize size = /* assume this exists */; UIImage *sourceImage = /* assume this exists */; NSString *textString = /* assume this exists */; char *text = [textString cStringUsingEncoding:NSMacOSRomanStringEncoding]; NSUInteger len = [textString lengthOfBytesUsingEncoding:cStringUsingEncoding:NSMacOSRomanStringEncoding]; NSMutableData *data = [NSMutableData dataWithLength:size.width*size.height*1]; CGContextRef context = CGBitmapContextCreate([data mutableBytes], size.width, size.height, 8, size.width, NULL, kCGImageAlphaOnly); CGContextSelectFont(context, "Gill Sans Bold", 64.0f, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetBlendMode(context, kCGBlendModeCopy); CGContextFillRect(context, overlay.bounds); CGContextSetBlendMode(context, kCGBlendModeClear); CGContextShowTextAtPoint(context, 16.0f, 16.0f, text, len); CGImageRef textImage = CGBitmapContextCreateImage(context); CGImageRef newImage = CGImageCreateWithMask(sourceImage.CGImage, textImage); UIImage *finalImage = [UIImage imageWithCGImage:newImage]; CGContextRelease(context); CFRelease(newImage); CFRelease(textImage); </code></pre> <p>Another way to do this involves putting the textImage into a new layer and setting that layer on your view's layer. (Remove the lines that create "newImage" and "finalImage".) Assuming this happens inside your view's code somewhere:</p> <pre><code>CALayer *maskLayer = [[CALayer alloc] init]; CGPoint position = CGPointZero; // layout the new layer position = overlay.layer.position; position.y *= 0.5f; maskLayer.bounds = overlay.layer.bounds; maskLayer.position = position; maskLayer.contents = (__bridge id)textImage; self.layer.mask = maskLayer; </code></pre> <p>There are more alternatives, some might be better (subclass UIImage and draw the text directly in clear mode after the superclass has done its drawing?).</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