macos 在 NSView 中添加边框和圆角矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5004960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:42:39  来源:igfitidea点击:

Adding border and Rounded Rect in the NSView

cocoamacosnsview

提问by Amitg2k12

In my Application , NSView should have rounded rect and border, i tried following

在我的应用程序中,NSView 应该有圆角矩形和边框,我尝试遵循

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

并在 InitWithframe 添加以下代码行

    [[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

我能猜到的另一种方法是,在 drawRect 中绘制边框,但看起来很复杂,谁能建议我任何其他方法

Kind Regards

亲切的问候

Rohan

罗汉

回答by Amitg2k12

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

回答by Richard Garside

For the layer properties to have any affect you need to set setWantsLayer on your NSView to YES first.

要使图层属性产生任何影响,您需要先将 NSView 上的 setWantsLayer 设置为 YES。

I have this in InitWithFrame for my view:

我在 InitWithFrame 中有这个供我查看:

[self setWantsLayer: YES];
[self.layer setBorderWidth: 2];
[self.layer setCornerRadius: 10];

回答by Grzegorz Krukiewicz-Gacek

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:

对上一个答案的一点改进。如果你不想子类化你的 NSView 如果它是你的控制器的基本视图,你也可以做这样的事情:

override func viewDidLoad() {
        super.viewDidLoad()

        self.view.wantsLayer = true
    }