NSTextCieldView中的NSTextField [英] NSTextField in NSTableCellView
问题描述
我有一个基于视图的NSTableView与自定义NSTableCellView。这个自定义NSTableCellView有几个标签(NSTextField)。 NSTableCellView的整个UI是在IB中构建的。
I have a view based NSTableView with a custom NSTableCellView. This custom NSTableCellView has several labels (NSTextField). The whole UI of the NSTableCellView is built in IB.
NSTableCellView可以处于正常状态和处于选定状态。
The NSTableCellView can be in a normal state and in a selected state. In the normal state all text labels should be black, in the selected state they should be white.
如何管理这个标签?
推荐答案
可能最简单的方法是子类化NSTextField并覆盖你的子类中的drawRect:方法。在这里你可以确定是否包含你的NSTextField实例的NSTableCellView实例当前是通过使用这个代码(我用于一个NSOutlineView,但它也应该与NSTableView使用)选择:
Probably the easiest way to accomplish this would be to subclass NSTextField and to override the drawRect: method in your subclass. There you can determine whether the NSTableCellView instance containing your NSTextField instances is currently selected by using this code (which I use with a NSOutlineView, but it should also work with NSTableView):
BOOL selected = NO;
id tableView = [[[self superview] superview] superview];
if ([tableView isKindOfClass:[NSTableView class]]) {
NSInteger row = [tableView selectedRow];
if (row != -1) {
id cellView = [tableView viewAtColumn:0 row:row makeIfNecessary:YES];
if ([cellView isEqualTo:[self superview]]) selected = YES;
}
}
然后绘制如下视图:
if (selected) {
// set your color here
// draw [self stringValue] here in [self bounds]
} else {
// call [super drawRect]
}
这篇关于NSTextCieldView中的NSTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!