2

NSTextField の背景として jpeg/png 画像を使用する方法はありますか?

4

1 に答える 1

10

1つの解決策:次のように簡単に実行できます:

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

結果:

背景付きの NSTextField


2 解決策: Yourを次の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

境界のある結果:

ボーダー付き

境界線なしの結果:

国境なし

于 2012-05-16T07:33:10.997 に答える