NSTextField with rounded corners?

2019-03-31 04:19发布

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

}

5条回答
别忘想泡老子
2楼-- · 2019-03-31 04:41

It is better to subclass NSTextFieldCell to draw rounded corners to preserve NSTextField functionality, e.g:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRoundedRect:cellFrame xRadius:CORNER_RADIUS yRadius:CORNER_RADIUS];
    [betterBounds addClip];
    [super drawWithFrame:cellFrame inView:controlView];
    if (self.isBezeled) {
        [betterBounds setLineWidth:2];
        [[NSColor colorWithCalibratedRed:0.510 green:0.643 blue:0.804 alpha:1] setStroke];
        [betterBounds stroke];
    }
}

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):

enter image description here

查看更多
时光不老,我们不散
3楼-- · 2019-03-31 04:41

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 :

enter image description here

查看更多
Fickle 薄情
4楼-- · 2019-03-31 04:42

Swift 3 version of @Verious codes, with properties editable in Interface Builder:

class RoundedTextFieldCell: NSTextFieldCell {

    @IBInspectable var borderColor: NSColor = .clear
    @IBInspectable var cornerRadius: CGFloat = 3

    override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
        let bounds = NSBezierPath(roundedRect: cellFrame, xRadius: cornerRadius, yRadius: cornerRadius)
        bounds.addClip()
        super.draw(withFrame: cellFrame, in: controlView)
        if borderColor != .clear {
            bounds.lineWidth = 2
            borderColor.setStroke()
            bounds.stroke()
        }
    }
}
查看更多
劫难
5楼-- · 2019-03-31 04:57

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):

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 20.0f)];
textField.layer.cornerRadius = 5.0f;
textField.layer.masksToBounds = YES;

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

查看更多
我命由我不由天
6楼-- · 2019-03-31 04:59

You are doing almost everything correct. You just need to change the textField's cell and radius which match. Take a look at this:

-(void)awakeFromNib {

    [[self cell] setBezelStyle: NSTextFieldRoundedBezel];
}


- (void)drawRect:(NSRect)dirtyRect
{

    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:10 yRadius:10] angle:90];

}

This is working for me nicely.

查看更多
登录 后发表回答