NSView Frame Color

I apply the border to NSView, but how can I change the bordercolor. Using NSColor with setBorderColor shows a warning. I want to use Orange Color in Border

    [self setWantsLayer:YES];
    self .layer.masksToBounds   = YES;    
    self.layer.borderWidth      = 6.0f ;

    [self.layer setBorderColor:CGColorGetConstantColor(kCGColorBlack)]; 

How to set other colors (excluding black and white) in Border

Regards, Haseena

+5
source share
2 answers

You need to convert to CGColorRef

NSColor *orangeColor = [NSColor orangeColor];

// Convert to CGColorRef
NSInteger numberOfComponents = [orangeColor numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[orangeColor colorSpace] CGColorSpace];    
[orangeColor getComponents:(CGFloat *)&components];    
CGColorRef orangeCGColor = CGColorCreate(colorSpace, components);

// Set border
self.view.layer.borderColor = orangeCGColor;

// Clean up
CGColorRelease(orangeCGColor);

Or, if you can require 10.8+, use [aColor CGColor]

+6
source

You can use NSBoxwhich is a subclass NSViewand is designed to handle these cases.

let box = NSBox()
box.boxType = .custom
box.alphaValue = 1
box.borderColor = NSColor.red
box.borderType = .lineBorder
box.borderWidth = 4
+1
source

All Articles