2009-03-21 10 views
5

J'essaie d'obtenir le "avis de caractères restants" apparaître dans mon NSTextField arrondi et je l'ai eu avec deux NSTextFields avec l'aide de l'Interface Builder et il ressemble déjà à ça:NSTextField avec "padding" sur la droite

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

Mais quand j'écris un peu plus on dirait que:

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

La seule chose que je pouvais penser est de sous-classe NSTextField et faire quelque chose avec elle il d oes ne dessine pas de texte sous le numéro mais je n'ai aucune idée de comment commencer et j'ai vraiment besoin d'aide.

Répondre

11

La manière la plus simple est probablement de sous-classer NSTextFieldCell et de remplacer -drawInteriorWithFrame:inView: et -selectWithFrame:inView:editor:delegate:start:length:.

Vous devrez décider de l'espace à allouer pour votre compte et dessiner dans l'espace abrégé. Quelque chose comme cet exemple de code devrait fonctionner bien que cela n'ait pas été testé dans un champ de texte arrondi.

Vous pouvez trouver plus d'informations sur le sous-classement NSCell dans le document 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

Merci pour l'aide, je vais essayer. – Jeena

+1

le lien semble brisé – rraallvv

+1

Le lien est maintenant: https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html – Christophe