The simplest way is probably to subclass NSTextFieldCell
and override -drawInteriorWithFrame:inView:
and -selectWithFrame:inView:editor:delegate:start:length:
.
You'll need to decide how much space to allocate for your count and draw in the abbreviated space. Something like this example code should work although this hasn't been tested in a rounded text field.
You can find more information about subclassing NSCell
in Apple's PhotoSearch example code.
- (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];
}
Ashley Clark
2009-03-22 09:10:16