Adding border and Rounded Rect in the NSView

我是研究僧i 提交于 2019-12-05 01:12:41

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
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!