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];
}
Merci pour l'aide, je vais essayer. – Jeena
le lien semble brisé – rraallvv
Le lien est maintenant: https://developer.apple.com/library/prerelease/content/samplecode/PhotoSearch/Introduction/Intro.html – Christophe