2013-02-04 23 views

Trả lời

4

Không. NSImage không chứa những thông minh như vậy. Bạn sẽ phải cắt, thay đổi kích cỡ và lắp ráp lại hình ảnh.

Bạn có thể xem xét tạo lớp con NSCustomImageRep thực hiện điều này, sau đó bạn có thể sử dụng để triển khai phiên bản OS X của cùng một phương pháp.

+0

Kể từ 10.10, nó có thể có ['capInsets'] (https://developer.apple.com/reference/appkit/nsimage/1520012-capinsets) var. Nó dường như không được sử dụng ở bất cứ đâu, nhưng nó ở đó. –

4

Bạn có thể sử dụng [NSImage drawAtPoint] để thay vào đó, như thế này:

@implementation NSImage (CapInsets) 

- (void)drawImageWithLeftCapWidth:(NSInteger)left topCapHeight:(NSInteger)top destRect:(NSRect)dstRect 
{ 
    NSSize imgSize = self.size; 
    [self drawAtPoint:dstRect.origin fromRect:NSMakeRect(0, 0, left, top) operation:NSCompositeSourceOver fraction:1]; 
    [self drawInRect:NSMakeRect(left, 0, dstRect.size.width-2*left, top) fromRect:NSMakeRect(left, 0, imgSize.width-2*left, top) operation:NSCompositeSourceOver fraction:1]; 
    [self drawAtPoint:NSMakePoint(dstRect.origin.x+dstRect.size.width-left, dstRect.origin.y) fromRect:NSMakeRect(imgSize.width-left, 0, left, top) operation:NSCompositeSourceOver fraction:1]; 
} 

@end 
2

NSImage đã nhận được một sự cải thiện nhẹ 10.10 (Yosemite). NSImage hiện có thuộc tính:

@property NSEdgeInsets capInsets 

Với thuộc tính này, bạn có thể đặt nắp chữ giống như trong iOS. Nếu bạn gọi [image drawInRect:rect], nó sẽ đưa các tài khoản này vào tài khoản. Lưu ý rằng điều này sẽ chỉ hoạt động trên các hệ thống đang chạy 10.10 trở lên; trên các hệ thống cũ hơn, nó sẽ đơn giản kéo giãn hình ảnh.