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

标签 objective-c macos cocoa user-interface appkit

我想做什么

我正在尝试向连接套件的 NSBrowser 添加就地编辑功能。我希望此行为在功能上和视觉上都类似于 Finder 的实现。

我想要的视觉效果

Finder's implementation

到目前为止我得到了什么

NSBrowserCell subclass

箭头表示 Finder 实现中的焦点环和单元格突出显示,而我的实现中则没有。

我试过了

  • 在 Controller 的drawInteriorWithFrame方法中设置单元格的背景色
  • 现场编辑也是如此
  • setFocusRingType:NSFocusRingTypeDefault 用于 Controller 和绘制方法中的字段编辑器和单元格
  • 在draw方法中手动绘制高亮颜色
  • 上述的各种组合,毫无疑问,有些我已经忘记了。

我所做的最好的事情是用高亮颜色为单元格图像周围的区域着色。

我在这里缺少一些基本知识吗?有人可以建议解决这个问题的起点吗? 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];
}

最佳答案

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

[NSGraphicsContext saveGraphicsState];
[[controlView superview] lockFocus];

NSSetFocusRingStyle(NSFocusRingAbove);
[[NSBezierPath bezierPathWithRect:NSInsetRect(frame,-1,-1)] fill];

[[controlView superview] unlockFocus];
[NSGraphicsContext restoreGraphicsState];

关于objective-c - 突出显示已编辑的 NSBrowserCell 并在其 NSText 上绘制聚焦环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9652905/

相关文章:

ios - 避免在同一个方法中编写连续的 'if' 语句

iphone - 如何停止 UITextField.rightView 的动画?

objective-c - 收听我的文本字段的值更改

eclipse - Mac OSX 中的 eclipse 中凭证存储失败

cocoa - 基于 NSDate 类型的属性对自定义对象的 NSArray 进行排序

objective-c - 类转储 z 生成空 header

iphone - 当成功 block 启动时,__block 弱崩溃

ios - 获取 uicollectionviewcell 的触摸位置

macos - Safari 中有没有办法将新标签的焦点默认设置为搜索框?

objective-c - 在 NSTreeController 或 NSOutlineView 中获取对象的 NSIndexPath 的简洁方法?