如何在NSImage周围绘制彩色边框?

Eug*_*din 6 macos cocoa nsimage

我做了一些研究,但我得到的所有答案都是针对iOS的,我如何在OSX应用程序中围绕NSImage绘制彩色边框?我尝试使用NSImage的imageView属性来设置它的边框宽度和颜色,但它似乎没有工作......

任何形式的帮助都非常感谢!

Hus*_*bir 12

尝试这样做而不创建子类也是可能的.您也可以相应地设置宽度和半径: -

[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];
Run Code Online (Sandbox Code Playgroud)


Mik*_*ael 5

您可以继承NSView并执行以下操作:

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor orangeColor] set];

    NSBezierPath *path = [NSBezierPath bezierPath];
    [path appendBezierPathWithRoundedRect:outerFrame xRadius:5 yRadius:5];
    [path setLineWidth:4.0];
    [path stroke];
}
Run Code Online (Sandbox Code Playgroud)

当然,如果你想要圆角,也可以改变半径值.