简体   繁体   English

NSTextFieldCell圆角描边不圆角

[英]NSTextFieldCell rounded corners stroke not rounded

I want to have an NSTextField with rounded corners, for that I subclassed my NSTextFieldCell, and used drawInteriorWithFrame:(NSRect) inView:(NSView *) My code looks like that : 我想要一个带有圆角的NSTextField,为此我将我的NSTextFieldCell子类化,并使用了drawInteriorWithFrame:(NSRect) inView:(NSView *)我的代码如下所示:

-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

//Color Declarations
NSColor* fillColor = [NSColor colorWithCalibratedRed: 1 green: 1 blue: 1 alpha: 1];
NSColor* strokeColor = [NSColor colorWithCalibratedRed: 0.679 green: 0.679 blue: 0.679 alpha: 1];

//Shadow Declarations
NSShadow* shadow = [[NSShadow alloc] init];
[shadow setShadowColor: strokeColor];
[shadow setShadowOffset: NSMakeSize(0.1, 0.1)];
[shadow setShadowBlurRadius: 4];

//Rounded Rectangle Drawing
NSBezierPath* roundedRectanglePath = [NSBezierPath bezierPathWithRoundedRect:cellFrame xRadius: 10 yRadius: 10];
[fillColor setFill];
[roundedRectanglePath fill];

//Rounded Rectangle Inner Shadow
NSRect roundedRectangleBorderRect = NSInsetRect([roundedRectanglePath bounds], -shadow.shadowBlurRadius, -shadow.shadowBlurRadius);
roundedRectangleBorderRect = NSOffsetRect(roundedRectangleBorderRect, -shadow.shadowOffset.width, -shadow.shadowOffset.height);
roundedRectangleBorderRect = NSInsetRect(NSUnionRect(roundedRectangleBorderRect, [roundedRectanglePath bounds]), -1, -1);

NSBezierPath* roundedRectangleNegativePath = [NSBezierPath bezierPathWithRect: roundedRectangleBorderRect];
[roundedRectangleNegativePath appendBezierPath: roundedRectanglePath];
[roundedRectangleNegativePath setWindingRule: NSEvenOddWindingRule];

[NSGraphicsContext saveGraphicsState];
{
    NSShadow* shadowWithOffset = [shadow copy];
    CGFloat xOffset = shadowWithOffset.shadowOffset.width + round(roundedRectangleBorderRect.size.width);
    CGFloat yOffset = shadowWithOffset.shadowOffset.height;
    shadowWithOffset.shadowOffset = NSMakeSize(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset));
    [shadowWithOffset set];
    [[NSColor grayColor] setFill];
    [roundedRectanglePath addClip];
    NSAffineTransform* transform = [NSAffineTransform transform];
    [transform translateXBy: -round(roundedRectangleBorderRect.size.width) yBy: 0];
    [[transform transformBezierPath: roundedRectangleNegativePath] fill];
}
[NSGraphicsContext restoreGraphicsState];

[strokeColor setStroke];
[roundedRectanglePath setLineWidth: 2];
[roundedRectanglePath stroke];

[super drawInteriorWithFrame:cellFrame inView:controlView];
}

The result looks great apart from borders which are not rounded. 除了没有四舍五入的边界外,结果看起来很棒。 Images are better than words : My NSTextField 图像胜于文字: My NSTextField

All help is accepted ! 接受所有帮助! :D Thank you in advance. :D预先谢谢你。

UPDATE I did a copy paste with the code of the site you said, but I'm still having the same trouble... NSTextField with on rounded corner 更新我用您说的站点代码进行了复制粘贴,但是我仍然遇到同样的麻烦... 带有圆角的NSTextField

You may simply choose the Border of Text Field in its Attributes Inspector if you want a text field with its all corners rounded. 如果要使文本字段的所有角都变圆,可以只在其“ Attributes Inspector选择“ Text FieldBorder ”。 Or to draw a text field with any one round corner, go through this 或者要绘制一个带有任意一个圆角的文本字段,请完成此操作

Also if you want draw a custom all round corner text field, just follow the steps in above link, but instead of drawing a bezier path with one round corner, simply draw a bezier path with all corners round using 另外,如果您想绘制一个自定义的圆角文本字段,只需按照上面的链接中的步骤操作,而不是绘制带有一个圆角的贝塞尔曲线,只需使用

 [NSBezierPath bezierPathWithRoundedRect:textfieldrect xRadius:10 yRadius:10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM