##1. ****给****NSImageView****增加**** ****backgroundColor
**** ****属性
1> 新建 BaseImageView
继承于 NSImageView
.h 中的代码
#import <Cocoa/Cocoa.h>
@interface BaseImageView : NSImageView
@property (nonatomic, strong) NSColor *backgroundColor;
@end
.m中的代码
- (void)setBackgroundColor:(NSColor *)backgroundColor {
_backgroundColor = backgroundColor;
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// 这个地方必须要判断,否则会被填充为黑色
if (_backgroundColor) {
NSRect rect = self.frame;
[_backgroundColor set];
[NSBezierPath fillRect:rect];
}
// Drawing code here.
}
外部调用
BaseImageView *imgBg = [[BaseImageView alloc] init];
imgBg.backgroundColor = [NSColor redColor];
##2. ****给****NSImageView****设置圆角
imgBg.wantsLayer = YES;
imgBg.layer.cornerRadius = 100;
网友评论