gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:39 25 4
gpt4 key购买 nike

我想做什么

我正在尝试向连接套件的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com