2014-02-24 103 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