NSTextField有圆角吗?

Dr.*_*eon 4 cocoa objective-c rounded-corners nstextfield

我正在尝试在NSTextField周围绘制圆角.

我已经分类NSTextField,尝试下面的代码,但没有任何结果......

有任何想法吗?

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

}
Run Code Online (Sandbox Code Playgroud)

Ver*_*ous 6

最好是子类NSTextFieldCell绘制圆角以保留NSTextField功能,例如:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

产生一个漂亮的圆形文本字段,完美地工作(如果你设置它首先绘制一个矩形边框,至少):

在此输入图像描述