【问题标题】:NSTextField with "padding" on the right右侧带有“填充”的 NSTextField
【发布时间】:2009-03-21 21:14:48
【问题描述】:
【问题讨论】:
标签:
objective-c
cocoa
nstextfield
【解决方案1】:
最简单的方法可能是继承NSTextFieldCell 并覆盖-drawInteriorWithFrame:inView: 和-selectWithFrame:inView:editor:delegate:start:length:。
您需要决定为计数分配多少空间并在缩略的空间中绘制。尽管尚未在圆形文本字段中进行测试,但类似此示例代码的内容应该可以工作。
您可以在 Apple 的 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];
}