Well, in principle you can add "#define kTopMargin 1.0" at the top, and into the cell methods add something like "cellFrame.origin.y += kTopMargin;" and "aRect.origin.y += kTopMargin;" However, this can look awful if the text does not fit properly.

In any case, Graham's solution might be a much better one.

Shlok Datye
Coding Turtle
http://codingturtle.com


On 07.08.2009, at 10:08, Joshua Garnham wrote:

Thanks that works perfectly!
One question though, what code would I need to add to also give it padding to the top of the text field?

Thanks Very Much,
Josh.

From: Shlok Datye <li...@codingturtle.com>
To: Joshua Garnham <joshua.garn...@yahoo.co.uk>
Cc: Cocoa Developers <cocoa-dev@lists.apple.com>
Sent: Friday, 7 August, 2009 11:00:21
Subject: Re: Adding Padding To The ‘left’ of a Text Field.

You can subclass NSTextFieldCell as in the following code, and apply it to your text field in Interace Builder.

----------------------------------------
// The header file

#import <Cocoa/Cocoa.h>

@interface CustomTextFieldCell : NSTextFieldCell
{
}

@end


// The implementation file

#import "CustomTextFieldCell.h"


#define kLeftMargin 10.0


@implementation CustomTextFieldCell

- (void)drawInteriorWithFrame:(NSRect)cellFrame
                       inView:(NSView *)controlView
{
  cellFrame.origin.x += kLeftMargin;
  cellFrame.size.width -= kLeftMargin;
  [super drawInteriorWithFrame:cellFrame
                        inView:controlView];
}

- (void)selectWithFrame:(NSRect)aRect
                 inView:(NSView *)controlView
                 editor:(NSText *)textObj
               delegate:(id)anObject
                  start:(NSInteger)selStart
                 length:(NSInteger)selLength
{
  aRect.origin.x += kLeftMargin;
  aRect.size.width -= kLeftMargin;
  [super selectWithFrame:aRect
                  inView:controlView
                  editor:textObj
                delegate:anObject
                   start:selStart
                  length:selLength];
}

- (NSRect)_focusRingFrameForFrame:(NSRect)frame
                        cellFrame:(NSRect)cellFrame
{
  return [[self controlView] bounds];
}

@end
----------------------------------------

Note that the last method (that takes care of properly displaying the focus ring) should probably not be relied upon too much, for obvious reasons. Anyone know of alternatives?

Shlok Datye
Coding Turtle
http://codingturtle.com


On 07.08.2009, at 07:18, Joshua Garnham wrote:

I have a text field with a background but to make it look right the text field needs to have some padding (or a slight indent) on the left side of it a bit like the NSSearchField (because of the search image). How would I give the text field some padding on the left?

Thanks,
Josh.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to