Reputation: 642
Is there any way of using a jpeg/png image as a background for NSTextField?
Upvotes: 2
Views: 1901
Reputation: 10198
1 solution: You can simply do it like this:
NSImage *image = ...; //image for background
[_textfieldOutlet setBackgroundColor:[NSColor colorWithPatternImage:image]];
Result:
2 solution: You can subclass Your NSTextField
like this:
#import "TextFieldSubclass.h"
@implementation TextFieldSubclass
- (void)awakeFromNib
{
[self setDrawsBackground:NO];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
NSImage *image = ...; //image for background
[image setFlipped:YES]; //image need to be flipped
//use this if You need borders
NSRect rectForBorders = NSMakeRect(2, 2, rect.size.width-4, rect.size.height-4);
[image drawInRect:rectForBorders fromRect:rectForBorders operation:NSCompositeSourceOver fraction:1.0];
//if You don't need borders use this:
//[image drawInRect:rect fromRect:rect operation:NSCompositeSourceOver fraction:1.0];
}
@end
Result with border:
Result without border:
Upvotes: 10