Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @GenjiApp GenjiApp revised this gist Jan 13, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NSImage+DrawAttributedString.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #import "NSImage+Additions.h"
    #import "NSImage+DrawAttributedString.h"

    @implementation NSImage (DrawAttributedString)

  2. @GenjiApp GenjiApp created this gist Dec 25, 2011.
    10 changes: 10 additions & 0 deletions NSImage+DrawAttributedString.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #import <AppKit/AppKit.h>

    @interface NSImage (DrawAttributedString)

    + (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString
    backgroundColor:(NSColor *)backgroundColor;
    + (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString;
    + (NSImage *)imageWithString:(NSString *)string;

    @end
    36 changes: 36 additions & 0 deletions NSImage+DrawAttributedString.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #import "NSImage+Additions.h"

    @implementation NSImage (DrawAttributedString)

    + (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString
    backgroundColor:(NSColor *)backgroundColor
    {
    NSSize boxSize = [attributedString size];
    NSRect rect = NSMakeRect(0.0, 0.0, boxSize.width, boxSize.height);
    NSImage *image = [[NSImage alloc] initWithSize:boxSize];

    [image lockFocus];

    [backgroundColor set];
    NSRectFill(rect);

    [attributedString drawInRect:rect];

    [image unlockFocus];

    return [image autorelease];
    }

    + (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString
    {
    return [self imageWithAttributedString:attributedString
    backgroundColor:[NSColor clearColor]];
    }

    + (NSImage *)imageWithString:(NSString *)string
    {
    NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:string] autorelease];
    return [self imageWithAttributedString:attrString];
    }

    @end