smorgan's answer points us in the right direction, but it took me quite a while to figure out how to restore the customized textfield's ability to display a background color -- you must call setBorder:YES
on the custom cell.
This is too late to help Joshua, but here's the how you implement the customized cell:
#import <Foundation/Foundation.h>
// subclass NSTextFieldCell
@interface InstructionsTextFieldCell : NSTextFieldCell {
}
@end
#import "InstructionsTextFieldCell.h"
@implementation InstructionsTextFieldCell
- (id)init
{
self = [super init];
if (self) {
// Initialization code here. (None needed.)
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (NSRect)drawingRectForBounds:(NSRect)rect {
// This gives pretty generous margins, suitable for a large font size.
// If you're using the default font size, it would probably be better to cut the inset values in half.
// You could also propertize a CGFloat from which to derive the inset values, and set it per the font size used at any given time.
NSRect rectInset = NSMakeRect(rect.origin.x + 10.0f, rect.origin.y + 10.0f, rect.size.width - 20.0f, rect.size.height - 20.0f);
return [super drawingRectForBounds:rectInset];
}
// Required methods
- (id)initWithCoder:(NSCoder *)decoder {
return [super initWithCoder:decoder];
}
- (id)initImageCell:(NSImage *)image {
return [super initImageCell:image];
}
- (id)initTextCell:(NSString *)string {
return [super initTextCell:string];
}
@end
(If, like Joshua, you only want an inset at the left, leave the origin.y and height as is, and add the same amount to the width -- not double -- as you do to the origin.x.)
Assign the customized cell like this, in the awakeFromNib method of the window/view controller that owns the textfield:
// Assign the textfield a customized cell, inset so that text doesn't run all the way to the edge.
InstructionsTextFieldCell *newCell = [[InstructionsTextFieldCell alloc] init];
[newCell setBordered:YES]; // so background color shows up
[newCell setBezeled:YES];
[self.tfSyncInstructions setCell:newCell];
[newCell release];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…