我有一个基于单元格的NSOutlineView,它显示NSTextFieldCell对象。
我希望对按键或按键事件做出响应,以便当文本包含某些预置关键字时,将NSTextFieldCell中包含的文本粗体化。实现这个目标的最优雅的方法是什么?我应该:
非常感谢所有的人的任何信息!
发布于 2013-07-16 01:24:51
找到了。
在awakeFromNib中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionToTakeOnKeyPress:) name:NSControlTextDidChangeNotification object:theNSOutlineViewThatContainsTheNSTextFieldCell]; 然后添加如下方法:
- (void) actionToTakeOnKeyPress: (id) sender
{
//will be called whenever contents of NSTextFieldCell change
}发布于 2017-03-20 14:12:09
要以仍然可以过滤掉的方式拦截按键,可以覆盖各种NSResponder消息,例如keyDown:或interpretKeyEvents:。
要做到这一点,需要使用NSTextView的子类作为字段编辑器。为此,一个子类NSTextFieldCell和重写fieldEditorForView:,返回子类(参见NSTextFieldCell在NSTableView中的自定义字段编辑器)。
以下是相关的代码摘录:
在子类NSTextFieldCell中(必须在接口生成器中为可编辑列分配该子类,或由NSTableViewDelegate的dataCellForTableColumn消息返回):
- (NSTextView *)fieldEditorForView:(NSView *)aControlView
{
if (!self.myFieldEditor) {
self.myFieldEditor = [[MyTextView alloc] init];
self.myFieldEditor.fieldEditor = YES;
}
return self.myFieldEditor;
}它还需要在@interface部分中声明属性:
@property (strong) MyTextView *myFieldEditor;然后在MyTextView中,它是NSTextView的一个子类
-(void)keyDown:(NSEvent *)theEvent
{
NSLog(@"MyTextView keyDown: %@", theEvent.characters);
static bool b = true;
if (b) { // this silly example only lets every other keypress through.
[super keyDown:theEvent];
}
b = !b;
}https://stackoverflow.com/questions/17663866
复制相似问题