How can you use an image as a background for a text box in Cocoa?

Can I use an image as a background for a text box in Cocoa? If so, how?

+3
source share
2 answers

I don't know if this is the β€œright” way to do it, but the first thing that comes to mind is to create a custom subclass of NSTextField that might look something like this:

- (void)awakeFromNib
{
    [self setDrawsBackground:NO];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [self lockFocus];
    [[NSImage imageNamed:@"<#image filename#>"] drawInRect:rect
        fromRect:rect
        operation:NSCompositeSourceOver
        fraction:1.0];
    [self unlockFocus];
}

Again, this is just a rough outline of the main parts.

, , , "" ( "" , ), NSTextField.

( ):

, ( , , Xcode , ).

NSTextField Objective-C (File β†’ New File...), , NSTextField NSObject, , :

#import <Cocoa/Cocoa.h>

@interface BGImageTextField : NSTextField
{
}

@end

, (, BGImageTextField.m), @implementation @end.

. -, Cocoa Programming for Mac OS X Aaron Hillegass - Cocoa, , Cocoa . -, , , , , , , , , NSTextField.

+4

NSTextField [NSColor clearColor] .

+3

Source: https://habr.com/ru/post/1706664/


All Articles