0

我创建了一个自定义 NSTextFieldCell 并- (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView在这里重写了我自己的绘图。但是,我在绘制背景时遇到了麻烦。如果不调用super背景,则不会清除背景,随后的绘图会产生类似涂抹效果的东西。设置 drawsBackground 时不会发生这种情况,因为在这种情况下,我可以用背景颜色填充 cellFrame。

- (void)drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView {
    if (self.drawsBackground) {
        [self.backgroundColor set];
    } else {
        [NSColor.clearColor set];
    }
    NSRectFill(cellFrame);

    [self.attributedStringValue drawInRect: cellFrame];
}

在此处输入图像描述

但是如果背景绘图被禁用,我该怎么做才能清除背景?我当然想让文本视图下的其他内容发光(所以,只是用超级视图的背景颜色擦除是没有解决方案的)。

4

1 回答 1

2

如果您尝试用 a 填充单元格,[NSColor clearColor]它会将其绘制为黑色。在不需要的时候尽量避免填充。您将能够删除您的super呼叫。

例子:

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    if (self.drawsBackground) {
        if (self.backgroundColor && self.backgroundColor.alphaComponent>0) {

            [self.backgroundColor set];
            NSRectFill(cellFrame);
        }
    }
    NSRect titleRect = [self titleRectForBounds:cellFrame];
    NSAttributedString *aTitle = [self attributedStringValue];
    if ([aTitle length] > 0) {
        [aTitle drawInRect:titleRect];
    }
}
于 2014-09-15T14:08:55.130 回答