Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

objective c - Adding Padding To The 'left' of a Text Field

I have a text field with a background but to make it look right the text field needs to have some padding on the left side of it a bit like the NSSearchField does. How would I give the text field some padding on the left?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...