Cách đơn giản nhất có thể là phân lớp NSTextFieldCell
và ghi đè -drawInteriorWithFrame:inView:
và -selectWithFrame:inView:editor:delegate:start:length:
.
Bạn sẽ cần phải quyết định số lượng không gian để phân bổ cho số lượng của bạn và vẽ trong không gian viết tắt. Một cái gì đó giống như mã ví dụ này nên làm việc mặc dù điều này đã không được thử nghiệm trong một trường văn bản được làm tròn.
Bạn có thể tìm thêm thông tin về phân lớp NSCell
trong Apple 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];
}
Nguồn
2009-03-22 09:10:16
Cảm ơn sự giúp đỡ, tôi sẽ thử điều đó. – Jeena
liên kết có vẻ bị hỏng – rraallvv
Liên kết hiện là: https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html – Christophe