Created
December 25, 2011 07:07
-
-
Save GenjiApp/1518837 to your computer and use it in GitHub Desktop.
NSImage's category for drawing attributed string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <AppKit/AppKit.h> | |
@interface NSImage (DrawAttributedString) | |
+ (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString | |
backgroundColor:(NSColor *)backgroundColor; | |
+ (NSImage *)imageWithAttributedString:(NSAttributedString *)attributedString; | |
+ (NSImage *)imageWithString:(NSString *)string; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSImage+DrawAttributedString.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment