2012-03-04 17 views
6

サブクラスNSImageViewがあり、丸みを帯びたコーナーで境界線を描きたい。それは動作しますが、私は画像コーナーも切り取る必要があります。私はボーダー/角の描画にこのコードを作成しているNSImageView丸みのあるコーナー+ストローク

enter image description here

私のスクリーンショットを参照してください。

- (void)drawRect:(NSRect)dirtyRect 
{ 
    [super drawRect:dirtyRect]; 

    NSColor *strokeColor; 
    if(self.isSelected) 
     strokeColor = [NSColor colorFromHexRGB:@"f9eca2"]; 
    else 
     strokeColor = [NSColor colorFromHexRGB:@"000000"]; 

    [strokeColor set];  
    [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(dirtyRect, 1, 1) xRadius:5 yRadius:5] stroke]; 
} 

イメージクリップを作成するにはどうすればよいですか?

EDIT:

まあ、私はそれを固定し、しかし、私はそれを行うにはその醜いように感じます。何でも賢い?

NEW CODE:

- (void)drawRect:(NSRect)dirtyRect 
{ 
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(dirtyRect, 2, 2) xRadius:5 yRadius:5]; 

    [path setLineWidth:4.0]; 
    [path addClip]; 

    [self.image drawAtPoint: NSZeroPoint 
       fromRect:dirtyRect 
       operation:NSCompositeSourceOver 
       fraction: 1.0]; 

    [super drawRect:dirtyRect]; 

    NSColor *strokeColor; 
    if(self.isSelected) 
    { 
     strokeColor = [NSColor colorFromHexRGB:@"f9eca2"]; 
    } 
    else 
     strokeColor = [NSColor colorFromHexRGB:@"000000"]; 

    [strokeColor set];  
    [NSBezierPath setDefaultLineWidth:4.0]; 
    [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(dirtyRect, 2, 2) xRadius:5 yRadius:5] stroke]; 
} 

答えて

3

は5ピクセルにもごNSImageViewの層の角の半径を設定し、YESにそのmaskToBoundsプロパティを設定します。 XCodeの8.3とスウィフトのため

+3

'NSImageView'は' clipsToBounds'プロパティを持っていない - それはNSないUI ImageViewのです。 – SteveF

+15

なぜstackoverflow上のすべての2番目の人は、objective-cコード== iOSを想定していますか? – strange

+2

AppKitでもclipToBoundsプロパティを持っている画像ビューのレイヤーについて話しているのを見ると、慎重に答えを読んでください。編集: ' - [NSView setWantsLayer:YES] 'を使用してレイヤーを有効にすることを忘れないでください。 – dvkch

0

更新答え3.1

import Cocoa 

class SomeViewController: NSViewController { 
    @IBOutlet weak var artwork: NSImageView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    artwork.wantsLayer = true // Use a layer as backing store for this view 
    artwork.canDrawSubviewsIntoLayer = true // Important, flatten all subviews into layer 
    artwork.layer?.cornerRadius = 4.0 
    artwork.layer?.masksToBounds = true // Mask layer 
    } 
} 
関連する問題