Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 在NSView中添加边框和圆角矩形_Cocoa_Macos_Nsview - Fatal编程技术网

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

Cocoa 在NSView中添加边框和圆角矩形,cocoa,macos,nsview,Cocoa,Macos,Nsview,在我的应用程序中,NSView应该具有圆形的rect和border,我尝试了以下操作 static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef colorSpace, NSColor *color) { NSColor *deviceColor = [color colorUsingColorSpaceName:

在我的应用程序中,NSView应该具有圆形的rect和border,我尝试了以下操作

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);
}
在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];
但没有任何效果,还有其他方法吗

我能猜到的另一种方法是,在drawRect中绘制边框,但它看起来非常复杂,有人能给我推荐其他方法吗

问候


Rohan

谢谢你看这个,这个逻辑对我有用

- (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];
}
-(void)drawRect:(NSRect)rect
{
if([self hasBorder])
[自绘制边框:rect];
}
-(无效)绘图边框:(NSRect)矩形{
NSRect frameRect=[自边界];
if(rect.size.height
要使图层特性产生任何影响,您需要先将NSView上的setWantsLayer设置为“是”

在InitWithFrame中,我的观点如下:

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

对前面的答案稍加改进。如果您不想将NSView子类化(如果它是控制器的基本视图),也可以执行以下操作:

override func viewDidLoad() {
        super.viewDidLoad()

        self.view.wantsLayer = true
    }