Pregunta

Estoy tratando de que el "aviso de caracteres restantes" aparezca dentro de mi NSTextField redondeado y lo obtuve con dos NSTextFields con la ayuda de Interface Builder y ya se ve así:

texto alternativo http://jeenaparadies.net/t/s/Twittia-NSTextField1.png

Pero cuando escribo un poco más se ve así:

texto alternativo http://jeenaparadies.net/t/s/Twittia-NSTextField2.png

Lo único que se me ocurre es crear una subclase de NSTextField y hacer algo con él para que no dibuje texto debajo del número, pero no tengo idea de cómo empezar y realmente necesito ayuda.

¿Fue útil?

Solución

La forma más sencilla es probablemente subclasificar NSTextFieldCell y anular -drawInteriorWithFrame:inView: y -selectWithFrame:inView:editor:delegate:start:length:.

Tendrás que decidir cuánto espacio asignar para contar y dibujar en el espacio abreviado.Algo como este código de ejemplo debería funcionar aunque no se ha probado en un campo de texto redondeado.

Puede encontrar más información sobre subclases. NSCell en Apple Código de ejemplo de PhotoSearch.

- (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];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top