Skip to content

Instantly share code, notes, and snippets.

@jjgod
Created December 13, 2009 15:05
Show Gist options
  • Save jjgod/255448 to your computer and use it in GitHub Desktop.
Save jjgod/255448 to your computer and use it in GitHub Desktop.
// textorize.m: Objective-C version of textorize
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *image;
NSBitmapImageRep *rep;
NSFont *font;
NSString *text;
NSDictionary *attributes;
NSSize size;
NSRect rect;
CGImageRef cgImage;
font = [NSFont fontWithName: @"The Sans Mono" size: 200.0];
text = [NSString stringWithUTF8String: argv[1]];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName, nil];
size = [text sizeWithAttributes: attributes];
size.width += 20;
size.height += 20;
rect = NSMakeRect(0, 0, size.width, size.height);
image = [[NSImage alloc] initWithSize: size];
[image lockFocus];
[[NSColor whiteColor] set];
NSRectFill(rect);
[[NSColor blackColor] set];
[text drawAtPoint: NSMakePoint(10, 10)
withAttributes: attributes];
[image unlockFocus];
cgImage = [image CGImageForProposedRect: NULL
context: nil
hints: nil];
NSLog(@"cgImage: %p", cgImage);
rep = [[NSBitmapImageRep alloc] initWithCGImage: cgImage];
[[rep representationUsingType: NSPNGFileType properties: nil] writeToFile: @"output.png" atomically: NO];
[rep release];
[image release];
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment