Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Inside the subclass of an NSTextFieldCell
- - (void)drawWithFrame:(NSRect)cellFrame
- inView:(NSView *)controlView
- {
- NSRect drawingRect = [self controlView].bounds;
- CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
- CGContextRef maskContextRef = CGBitmapContextCreate(
- NULL,
- drawingRect.size.width,
- drawingRect.size.height,
- 8,
- drawingRect.size.width,
- colorspace,
- 0);
- CGColorSpaceRelease(colorspace);
- // Switch to the context for drawing
- NSGraphicsContext *maskContext = [NSGraphicsContext graphicsContextWithGraphicsPort:maskContextRef
- flipped:NO];
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext:maskContext];
- // Draw a black background
- [[NSColor blackColor] setFill];
- CGContextFillRect(maskContextRef, drawingRect);
- // Draw the text right-way-up (non-flipped context)
- [[self stringValue] drawInRect:drawingRect
- withAttributes:@{
- NSFontAttributeName : self.font,
- NSForegroundColorAttributeName : [NSColor whiteColor]
- }];
- // Switch back to the window's context
- [NSGraphicsContext restoreGraphicsState];
- // Create an image mask from what we've drawn so far
- CGImageRef alphaMask = CGBitmapContextCreateImage(maskContextRef);
- // Draw a white background in the window
- CGContextRef cellContext = [[NSGraphicsContext currentContext] graphicsPort];
- // Draw the image, clipped by the mask
- CGContextSaveGState(cellContext);
- CGContextClipToMask(cellContext, NSRectToCGRect(drawingRect), alphaMask);
- if (!_innerShadow) {
- _innerShadow = [[NSShadow alloc] init];
- _innerShadow.shadowColor = [NSColor blackColor];
- _innerShadow.shadowBlurRadius = 3.0;
- _innerShadow.shadowOffset = NSMakeSize(0, 0);
- }
- //[_innerShadow set];
- [[self stringValue] drawInRect:drawingRect
- withAttributes:@{
- NSFontAttributeName : self.font,
- NSForegroundColorAttributeName : [NSColor blackColor]
- }];
- CGContextRestoreGState(cellContext);
- CGImageRelease(alphaMask);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement