2013-10-28 61 views
5

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

任何形式的帮助,高度赞赏!

回答

11

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

[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

[imgView setWantsLayer:YES];做到了!谢谢 –

+0

如何为NSImageView Mr.Hussain设置背景图片。我试图在上面的代码中用[NSColor colorWithPatternImage:....]替换colorWithCalibrated,但这似乎不起作用,请帮助我,谢谢:) –

+0

为什么使用colorWithPatternImage? –

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

,当然,如果你想圆角或不改变取决于半径值。

+0

谢谢你,但有可能没有子类化呢? –

1

斯威夫特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 
0

迅速

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