2013-10-28 60 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() 
}