Advertisement
thecrypticace

CG Masks Don't Like Me

Feb 11th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Inside the subclass of an NSTextFieldCell
  2.  
  3. - (void)drawWithFrame:(NSRect)cellFrame
  4.                inView:(NSView *)controlView
  5. {
  6.     NSRect drawingRect = [self controlView].bounds;
  7.    
  8.     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
  9.     CGContextRef maskContextRef = CGBitmapContextCreate(
  10.                                                         NULL,
  11.                                                         drawingRect.size.width,
  12.                                                         drawingRect.size.height,
  13.                                                         8,
  14.                                                         drawingRect.size.width,
  15.                                                         colorspace,
  16.                                                         0);
  17.     CGColorSpaceRelease(colorspace);
  18.    
  19.    
  20.     // Switch to the context for drawing
  21.     NSGraphicsContext *maskContext = [NSGraphicsContext graphicsContextWithGraphicsPort:maskContextRef
  22.                                                                                 flipped:NO];
  23.     [NSGraphicsContext saveGraphicsState];
  24.     [NSGraphicsContext setCurrentContext:maskContext];
  25.    
  26.     // Draw a black background
  27.     [[NSColor blackColor] setFill];
  28.     CGContextFillRect(maskContextRef, drawingRect);
  29.    
  30.     // Draw the text right-way-up (non-flipped context)
  31.     [[self stringValue] drawInRect:drawingRect
  32.                     withAttributes:@{
  33.               NSFontAttributeName : self.font,
  34.    NSForegroundColorAttributeName : [NSColor whiteColor]
  35.      }];
  36.    
  37.     // Switch back to the window's context
  38.     [NSGraphicsContext restoreGraphicsState];
  39.    
  40.     // Create an image mask from what we've drawn so far
  41.     CGImageRef alphaMask = CGBitmapContextCreateImage(maskContextRef);
  42.    
  43.     // Draw a white background in the window
  44.     CGContextRef cellContext = [[NSGraphicsContext currentContext] graphicsPort];
  45.  
  46.     // Draw the image, clipped by the mask
  47.     CGContextSaveGState(cellContext);
  48.     CGContextClipToMask(cellContext, NSRectToCGRect(drawingRect), alphaMask);
  49.    
  50.     if (!_innerShadow) {
  51.         _innerShadow = [[NSShadow alloc] init];
  52.         _innerShadow.shadowColor = [NSColor blackColor];
  53.         _innerShadow.shadowBlurRadius = 3.0;
  54.         _innerShadow.shadowOffset = NSMakeSize(0, 0);
  55.     }
  56.     //[_innerShadow set];
  57.    
  58.     [[self stringValue] drawInRect:drawingRect
  59.                     withAttributes:@{
  60.               NSFontAttributeName : self.font,
  61.    NSForegroundColorAttributeName : [NSColor blackColor]
  62.      }];
  63.    
  64.     CGContextRestoreGState(cellContext);
  65.     CGImageRelease(alphaMask);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement