I'm trying to draw rounded corners around an NSTextField.
I've subclassed NSTextField
, tried the code below, but without any result...
Any ideas?
- (void)drawRect:(NSRect)dirtyRect
{
// black outline
NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
NSGradient *gradient = nil;
if ([NSApp isActive]) {
gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.24 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.374 alpha:1.0]];
}
else {
gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.55 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0.558 alpha:1.0]];
}
[gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:5 yRadius:5] angle:90];
}
It is better to subclass
NSTextFieldCell
to draw rounded corners to preserveNSTextField
functionality, e.g:Yields a nice rounded text field that works perfectly (if you had set it to draw a rectangle bezel in the first place, at least):
The easiest one is to do with interface builder, unless you want the corners rounded by some units:
Simply select the border as rounded one :
Swift 3 version of @Verious codes, with properties editable in Interface Builder:
Include the QuartzCore framework in your project. Import
<QuartzCore/QuartzCore.h>
and use something like the following code (just from the top of my head):Voilà, a text field with rounded corners :)
Edit: just realized this question is related to Mac OS X. Please check the following link: http://cocoatricks.com/2010/06/a-better-looking-text-field/
Edit 2: created an example project with the rounded text field: http://dl.dropbox.com/u/6487838/RoundedTextField.zip
You are doing almost everything correct. You just need to change the textField's cell and radius which match. Take a look at this:
This is working for me nicely.