Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding border and Rounded Rect in the NSView

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

like image 599
Amitg2k12 Avatar asked Feb 15 '11 14:02

Amitg2k12


1 Answers

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];
}
like image 100
Amitg2k12 Avatar answered Nov 16 '22 03:11

Amitg2k12