3

如果用户在编辑 NSTextFieldCell 时输入了某些文本,我正在尝试将 NSTextFieldCell 的内容加粗。

到目前为止,我有这个:

在 awakeFromNib 中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldCellDidChange:)  name:NSControlTextDidChangeNotification object:theNSTableView];

方法:

- (void) textFieldCellDidChange: (id) sender
{
    //successfully captures contents of the NSTextFieldCell prior to the 
    //start of editing
    NSString * textOfMyNSTextFieldCell = [myNSTextFieldCell stringValue];

    //attempts to capture the current edited contents of the NSTextFieldCell while
    //editing is still in progress:
    //but DOES NOT YET WORK:
    NSText *fieldEditor = [myTableView currentEditor];
    textOfMyNSTextFieldCell = [fieldEditor string];
}

是否可以在编辑仍在进行时捕获 NSTextFieldCell 的编辑内容?

4

1 回答 1

2

要在进行更改后获取 NSTextFieldCell 的 stringValue,请将 NSTextFieldCell 子类化并实现:

- (void) textDidChange:(NSNotification*)notification {
    NSLog(@"NSTextFieldCell noticed that text did change to: %@", self.stringValue) ;
    NSLog(@"Was notified by: \n%@", notification.object) ;
    NSLog(@"which is its controlView's currentEditor: \n%@", ((NSTextField*)self.controlView).currentEditor) ;
}
于 2013-07-17T16:32:45.207 回答