我试图让“剩余的字符通知”显示在我的四舍五入的NSTextField中,在界面生成器的帮助下,我用两个NSTextFields得到了它,它看起来已经是这样的:
alt text http://jeenaparadies.net/t/s/Twittia-NSTextField1.png
但是当我多写一点的时候,它看起来是这样的:
alt text http://jeenaparadies.net/t/s/Twittia-NSTextField2.png
我唯一能想到的就是将NSTextField子类化,并用它做一些事情,这样它就不会在数字下绘制文本,但我不知道如何开始,需要一些帮助。
发布于 2009-03-22 09:10:16
最简单的方法可能是继承NSTextFieldCell的子类并覆盖-drawInteriorWithFrame:inView:和-selectWithFrame:inView:editor:delegate:start:length:。
您需要决定为您的计数分配多少空间,并在缩写空间中绘制。像这样的示例代码应该可以工作,尽管这还没有在四舍五入的文本字段中进行测试。
你可以在苹果的PhotoSearch example code中找到更多关于NSCell子类化的信息。
- (void)drawInteriorWithFrame:(NSRect)bounds inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:bounds];
NSRect countRect = [self countAreaRectForBounds:bounds];
titleRect = NSInsetRect(titleRect, 2, 0);
NSAttributedString *title = [self attributedStringValue];
NSAttributedString *count = [self countAttributedString];
if (title)
[title drawInRect:titleRect];
[count drawInRect:countRect];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
NSRect selectFrame = aRect;
NSRect countRect = [self countAreaRectForBounds:aRect];
selectFrame.size.width -= countRect.size.width + PADDING_AROUND_COUNT;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__textChanged:) name:NSTextDidChangeNotification object:textObj];
[super selectWithFrame:selectFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
- (void)endEditing:(NSText *)editor {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextDidChangeNotification object:editor];
[super endEditing:editor];
}
- (void)__textChanged:(NSNotification *)notif {
[[self controlView] setNeedsDisplay:YES];
}https://stackoverflow.com/questions/670015
复制相似问题