有没有办法改变nsimagecell背景颜色?

时间:2015-12-07 21:56:43

标签: cocoa

有没有办法可以改变nsimagecell的背景颜色?我可以使用setBackgroundColor轻松地为文本字段单元格做这个...这个解决方案的一些代码会很好! :)

2 个答案:

答案 0 :(得分:0)

1)需要创建NSImageCell的子类并在drawInrect

中填充颜色

或 2)没有设置单元格的BGColor,但看起来像那样

NSImageView *image =[[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 100, 100)];
[image setWantsLayer:YES];
[image.layer setBackgroundColor:[NSColor whiteColor].CGColor];

答案 1 :(得分:0)

这是答案,分享我的第一个子课......

这个NSImageCell响应setBackgroundColor! :)

//
//  colorImageCell.h
//  OldFileRemover
//
//  Created by Riz Khan on 16/12/15.
//  Copyright © 2015 Riz Khan. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface colorImageCell : NSImageCell

- (void)setImage:(NSImage *)theImage;

- (void)setBackgroundColor:(NSColor *)theColor;

@end


//
//  colorImageCell.m
//  OldFileRemover
//
//  Created by Riz Khan on 16/12/15.
//  Copyright © 2015 Riz Khan. All rights reserved.
//

#import "colorImageCell.h"

@implementation colorImageCell

NSColor *cellBGColor;
NSImage *iconImage;

- (id)init
{
    self = [super init];

    cellBGColor = [NSColor whiteColor];

    iconImage = nil;

    return self;
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    // if you want the image cell to do what it normally would do as if you hadn't subclassed it, call super:
    [super drawWithFrame:cellFrame inView:controlView];

    // do the rest of your custom drawing (if any)

    [cellBGColor setFill];

    NSRectFill(cellFrame);

    [iconImage drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f respectFlipped:YES hints:nil];
}

- (void)setImage:(NSImage *)theImage
{
    iconImage = theImage;
}

- (void)setBackgroundColor:(NSColor *)theColor
{
    cellBGColor = theColor;
}

@end