2012-05-16 138 views

回答

9

1解决方法:你可以简单地做这样的:

NSImage *image = ...; //image for background 
[_textfieldOutlet setBackgroundColor:[NSColor colorWithPatternImage:image]]; 

结果:

NSTextField with background


2溶液:你也可以继承你的NSTextField这样:

#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 

结果与边框:

with border

结果无边框:

without border

+0

谢谢!像魅力一样工作。 – rsharma

+0

@etheory不客气。 –