NSTextField有圆角吗?

时间:2012-03-29 17:23:50

标签: objective-c cocoa 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];

}

6 个答案:

答案 0 :(得分: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];
    }
}

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

enter image description here

答案 1 :(得分:4)

你几乎所有事情都是正确的。您只需要更改textField的单元格和匹配的半径。看看这个:

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

}

这很适合我。

答案 2 :(得分:3)

Swift 3版本的@Verious代码,其属性可在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()
        }
    }
}

答案 3 :(得分:2)

在项目中包含QuartzCore框架。导入<QuartzCore/QuartzCore.h>并使用类似下面的代码(仅从头顶开始):

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

Voilà,一个带圆角的文字字段:)

修改:刚刚意识到此问题与Mac OS X有关。请检查以下链接:http://cocoatricks.com/2010/06/a-better-looking-text-field/

编辑2:使用圆角文本字段创建了一个示例项目:http://dl.dropbox.com/u/6487838/RoundedTextField.zip

答案 4 :(得分:1)

最简单的方法是使用界面构建器,除非您希望角点按某些单位舍入:

只需将边框选为圆角边框:

enter image description here

答案 5 :(得分:0)

我确实喜欢这个,并且它像魅力一样工作。

yourLableName.wantsLayer = YES;
yourLableName.layer.cornerRadius = yourLableName.frame.size.width/2;