Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know if this is the "correct" way to do it, but the first thing that comes to mind would be to make a custom subclass of NSTextField, which might look roughly like this:</p> <pre><code>- (void)awakeFromNib { [self setDrawsBackground:NO]; } - (void)drawRect:(NSRect)rect { [super drawRect:rect]; [self lockFocus]; [[NSImage imageNamed:@"&lt;#image filename#&gt;"] drawInRect:rect fromRect:rect operation:NSCompositeSourceOver fraction:1.0]; [self unlockFocus]; } </code></pre> <p>Again, that's a just rough outline of the essential parts.</p> <p>Anyways, like I said, I'm not sure if this really the "correct" way to do it (or if there is even a "correct" way, for that matter), but this will give you a background image for your NSTextField.</p> <p><em>Edit in response to Joshua's comment (I'm not going to have enough room in that tiny little comment box):</em></p> <p>To add the image into your project, you'd drag it from wherever it is into the project window (the main list of files in the middle of the project window, although depending on how you've set up your Xcode editing environment, this might be different for you).</p> <p>In order to subclass <code>NSTextField</code>, you would want to create a new Objective-C class file (File -> New File…), but edit the header so that the class inherits from <code>NSTextField</code> instead of <code>NSObject</code>. In other words, your header file might look like this:</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface BGImageTextField : NSTextField { } @end </code></pre> <p>As for the rest of the code, you would want to add that in the main body of the implementation file (<code>BGImageTextField.m</code>, for example), specifically in between the <code>@implementation</code> and <code>@end</code> keywords.</p> <p>I'd also like to mention two things. First, I'd recommend picking up a copy of <em>Cocoa Programming for Mac OS X</em>, by Aaron Hillegass—it covers most of the Cocoa basics that I just went over, and is one of the best ways to learn Cocoa in general. Secondly, although my approach works, it's probably not the best approach—especially since I just recently found <a href="https://stackoverflow.com/questions/606319/subclassing-a-nstextfield/607979#607979">this post</a>, which seems to hint at a better way of extending NSTextField.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload