2009-03-21 5 views
5

둥근 NSTextField에 "나머지 문자 알림"을 ​​표시하려고하는데 인터페이스 작성기의 도움으로 두 개의 NSTextFields를 얻었으며 이미 다음과 같이 보입니다 :오른쪽에 "채우기"가있는 NSTextField

내가 조금 쓸 때

alt text http://jeenaparadies.net/t/s/Twittia-NSTextField1.png

는하지만 더는 그 모양 : 내가 생각할 수

alt text http://jeenaparadies.net/t/s/Twittia-NSTextField2.png

유일한 것은 NSTextField있는 서브 클래스하고 d를, 그래서 그것으로 뭔가를 할 것입니다 oes는 숫자 아래에 텍스트를 그 으려하지 않지만 시작하는 법을 알지 못하고 실제로 도움이 필요합니다.

답변

11

가장 간단한 방법은 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]; 
} 
+0

도움을 주셔서 감사합니다. – Jeena

+1

링크가 깨졌습니다. – rraallvv

+1

링크가 지금 있습니다 : https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html – Christophe

관련 문제