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

时间:2013-10-28 21:21:24

标签: macos cocoa nsimage

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

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

4 个答案:

答案 0 :(得分:11)

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

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

答案 1 :(得分: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];
}

当然会根据您是否需要圆角来更改半径值。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

swift

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