2
有沒有使用jpeg/png圖像作爲NSTextField背景的方法?使用圖像作爲NSTextField的背景
有沒有使用jpeg/png圖像作爲NSTextField背景的方法?使用圖像作爲NSTextField的背景
1解決方法:你可以簡單地做這樣的:
NSImage *image = ...; //image for background
[_textfieldOutlet setBackgroundColor:[NSColor colorWithPatternImage:image]];
結果:
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
結果與邊框:
結果無邊框:
謝謝!像魅力一樣工作。 – rsharma
@etheory不客氣。 –