가장 간단한 방법은 NSTextFieldCell
을 서브 클래 싱하고 -drawInteriorWithFrame:inView:
및 -selectWithFrame:inView:editor:delegate:start:length:
을 무시하는 것입니다.
계산에 할당 할 공간을 정하고 축약 된 공간을 그려야합니다. 둥근 텍스트 필드에서 테스트되지 않았지만이 예제 코드와 같은 것이 작동합니다.
NSCell
서브 클래 싱에 대한 자세한 내용은 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];
}
도움을 주셔서 감사합니다. – Jeena
링크가 깨졌습니다. – rraallvv
링크가 지금 있습니다 : https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html – Christophe