How to draw a colored frame around NSImage?

I did some research, but all the answers I received were for iOS, how can I draw a color frame around NSImage in an OSX app? I tried using the imageView property for NSImage to set its width and border color, but it doesn't seem to work ...

Any help is appreciated!

+5
source share
4 answers

Try this without creating a subclass and this is possible. You can also set the radius of the width and: -

[imgView setWantsLayer:YES]; imgView.layer.borderWidth = 1.0; imgView.layer.cornerRadius = 8.0; imgView.layer.masksToBounds = YES; CGColorRef color = CGColorRetain([NSColor colorWithCalibratedRed:0 green:100 blue:0 alpha:0.5f].CGColor); [imgView.layer setBorderColor:color]; 
+11
source

You can subclass NSView and do something like this:

 - (void)drawRect:(NSRect)dirtyRect { [[NSColor orangeColor] set]; NSBezierPath *path = [NSBezierPath bezierPath]; [path appendBezierPathWithRoundedRect:outerFrame xRadius:5 yRadius:5]; [path setLineWidth:4.0]; [path stroke]; } 

and of course change the radius values ​​depending if you want rounded corners or not.

+5
source

In Swift 3 :

 imagePreview.layer!.borderWidth = 10.0 imagePreview.layer!.cornerRadius = 8.0 imagePreview.layer!.masksToBounds = true let color: CGColor = NSColor.blue.cgColor imagePreview.layer!.borderColor = color 
+1
source

In fast :

 override func draw(_ dirtyRect: NSRect) { // Draw Border borderColor.set() let borderPath = NSBezierPath(rect: dirtyRect) borderPath.lineWidth = 2.0 borderPath.stroke() } 
0
source

Source: https://habr.com/ru/post/979384/


All Articles