2014-02-24 72 views
0

我想有一个的NSTextField圆角,对于我子类我NSTextFieldCell,并用drawInteriorWithFrame:(NSRect) inView:(NSView *) 我的代码看起来像这样:NSTextFieldCell圆角行程不圆

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

结果从边框看起来很大除这些不是圆形的。图片比文字好:My NSTextField

所有帮助被接受! :D先谢谢你。

UPDATE 我做了一个副本,你说该网站的代码粘贴,但我仍然有同样的烦恼...... NSTextField with on rounded corner

+0

重复的问题,答案在这里:http://stackoverflow.com/questions/9930329/nstextfield-with-rounded-corners –

回答

0

您可以简单地选择Text FieldBorder在其Attributes Inspector如果您想要一个四舍五入的文本字段。或者用任何一个圆角画一个文本字段,通过this

此外,如果你想绘制一个自定义的全角文本字段,只需按照上面的链接中的步骤,而不是一个绘制一个贝塞尔路径角落,只需战平各个角落贝塞尔路径绕行

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

其实,我不t想要有圆角的角落,但是它更具视觉效果,圆角更多...... – Starboard