首页 文章

突出显示已编辑的NSBrowserCell并在其NSText上绘制一个聚焦环?

提问于
浏览
4

What I'm trying to do

我正在尝试向Connection Kit的NSBrowser添加编辑就地功能 . 我希望这种行为在功能和视觉上与Finder的实现类似 .

The visual effect I'm aiming for

Finder's implementation

What I've got so far

NSBrowserCell subclass

箭头表示Finder实施中的焦点环和单元格突出显示,以及我的缺乏 .

I have tried

  • 在控制器的 drawInteriorWithFrame 方法中设置单元格的背景颜色

  • 对于字段编辑器也是如此

  • setFocusRingType:NSFocusRingTypeDefault 用于控制器和绘图方法中的字段编辑器和单元格

  • 在绘图方法中手动绘制突出显示颜色

  • 以上的各种组合,无疑有些我已经忘记了 .

我所做的最好的就是让细胞图像周围的区域以高亮颜色着色 .

我在这里缺少一些根本吗?有人可以建议一个接近这个的起点吗?是 drawInteriorWithFrame 这个地方吗?

我编辑工作正常 - 我只是在视觉方面遇到麻烦 .

允许编辑的代码:

// In the main controller
int selectedColumn = [browser selectedColumn];
int selectedRow = [browser selectedRowInColumn:selectedColumn];
NSMatrix *theMatrix = [browser matrixInColumn:selectedColumn];
NSRect cellFrame = [theMatrix cellFrameAtRow:selectedRow column:0];

NSText *fieldEditor = [[browser window] fieldEditor:YES 
                                          forObject:editingCell];

[cell editWithFrame:cellFrame 
             inView:theMatrix
             editor:fieldEditor 
           delegate:self
              event:nil];

在我的NSBrowserCell的子类中:

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {  

    image = [[self representedObject] iconWithSize:[self imageSize]];

    [self setImage:image];

    NSRect imageFrame, highlightRect, textFrame;

    // Divide the cell into 2 parts, the image part (on the left) and the text part.
    NSDivideRect(cellFrame, &imageFrame, &textFrame, ICON_INSET_HORIZ + ICON_TEXT_SPACING + [self imageSize].width, NSMinXEdge);
    imageFrame.origin.x += ICON_INSET_HORIZ;
    imageFrame.size = [self imageSize];

    [super drawInteriorWithFrame:cellFrame inView:controlView];
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
    NSRect imageRect, textRect;
    NSDivideRect(aRect , &imageRect, &textRect, 20, NSMinXEdge);
    self.editing = YES;
    [super editWithFrame: textRect inView: controlView editor:textObj delegate:anObject event:theEvent];
}

2 回答

  • 3

    你必须自己画出聚焦环 . 在NSBrowserCell子类中的drawWithFrame中添加以下内容

    [NSGraphicsContext saveGraphicsState];
    [[controlView superview] lockFocus];
    
    NSSetFocusRingStyle(NSFocusRingAbove);
    [[NSBezierPath bezierPathWithRect:NSInsetRect(frame,-1,-1)] fill];
    
    [[controlView superview] unlockFocus];
    [NSGraphicsContext restoreGraphicsState];
    
  • -1

    尝试子类化字段编辑器对象并覆盖drawRect函数,如下所示:

    - (void)drawRect:(NSRect)rect {
    
        [super drawRect:rect];
    
        NSSetFocusRingStyle(NSFocusRingOnly);
    
        NSRectFill([self bounds]);
    
    }
    

    希望这可以帮助 .

相关问题