In my Application , NSView should have rounded rect and border, i tried following
static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
colorSpace, NSColor *color)
{
NSColor *deviceColor = [color colorUsingColorSpaceName:
NSDeviceRGBColorSpace];
float components[4];
[deviceColor getRed: &components[0] green: &components[1] blue:
&components[2] alpha: &components[3]];
return CGColorCreate (colorSpace, components);
}
and in InitWithframe added following lines of Code
[[self layer] setCornerRadius:505];
[[self layer] setBorderWidth:500.0];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGColorRef cgColor = CGColorCreateFromNSColor (colorSpace, [NSColor whiteColor]);
CGColorSpaceRelease (colorSpace);
[[self layer] setBorderColor:cgColor];
but no effects at all, is there any other method,
Another approach what i could guess is , in drawRect draw border and but it seems very complex, can anyone suggest me any other method
Kind Regards
Rohan
Thanks for looking at this, this logic worked for me,
- (void)drawRect:(NSRect)rect
{
if([self hasBorder])
[self drawBorder:rect];
}
-(void)drawBorder:(NSRect)rect{
NSRect frameRect = [self bounds];
if(rect.size.height < frameRect.size.height)
return;
NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);
NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:10 yRadius:10];
[textViewSurround setLineWidth:BORDER_WIDTH];
[pBorderColor set];
[textViewSurround stroke];
}
For the layer properties to have any affect you need to set setWantsLayer on your NSView to YES first.
I have this in InitWithFrame for my view:
[self setWantsLayer: YES];
[self.layer setBorderWidth: 2];
[self.layer setCornerRadius: 10];
A little enhancement to the previous answer. If you don't want to subclass your NSView if it's the base view of your controller you can also do something like this:
override func viewDidLoad() {
super.viewDidLoad()
self.view.wantsLayer = true
}
来源:https://stackoverflow.com/questions/5004960/adding-border-and-rounded-rect-in-the-nsview