美文网首页Cocoa 框架
NSImageView 详解

NSImageView 详解

作者: 提笔挥墨 | 来源:发表于2016-07-28 13:50 被阅读533次

##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;

相关文章

网友评论

    本文标题:NSImageView 详解

    本文链接:https://www.haomeiwen.com/subject/yvsajttx.html