2012-01-19 16 views
9

"Now Playing" está en una línea en UIBarButtonItem. Tengo que ponerlo en dos líneas, como "Ahora" es Ay y "Reproducción" se han escrito en bottom.I la siguiente línea superior de código: -¿Cómo podemos poner dos líneas en UIBarButtonItem en la barra de navegación

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 

           initWithTitle:@"Now Playing" 

           style:UIBarButtonItemStyleBordered 

           target:self 

           action:@selector(flipView)]; 


    self.navigationItem.rightBarButtonItem = flipButton; 

así que quiero salto de línea en el medio de la PU "Jugando ahora". Asi es que, por favor ayudame.

+0

¿Qué es "Now \ n Playing"? – barley

+0

Lo siento, no funciona. –

+0

Bien. Parece que es un duplicado. Pero parece que no hay una manera fácil ... http://stackoverflow.com/questions/2614098/how-can-i-make-the-text-of-a-uibarbuttonitem-wrap-to-two -lines – barley

Respuesta

4
- (void)createCustomRightBarButton_ 
{ 
    UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease]; 
    addCustomLabel.text [email protected]"Now\nPlaying"; 
    addCustomLabel.textColor = [UIColor whiteColor]; 
    addCustomLabel.font = [UIFont boldSystemFontOfSize:11]; 
    addCustomLabel.numberOfLines = 2; 
    addCustomLabel.backgroundColor = [UIColor clearColor]; 
    addCustomLabel.textAlignment = UITextAlignmentCenter; 

    CGSize size = addCustomLabel.bounds.size; 

    UIGraphicsBeginImageContext(size);  
    CGContextRef context = UIGraphicsGetCurrentContext();  
    [addCustomLabel.layer renderInContext: context]; 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 
    UIImage * img = [UIImage imageWithCGImage: imageRef]; 
    CGImageRelease(imageRef); 
    CGContextRelease(context); 

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img 
                      style:UIBarButtonItemStyleBordered 
                      target:self 
                      action:@selector(flipView)] 
               autorelease]; 
} 
+0

I encontró que textColor se ignoraba y el elemento de navegación siempre aplicaba el azul predeterminado. Si desea cambiar el color, configure el tintColor de UIBarButtonItem. – Sky

+0

Especialmente en dispositivos más nuevos, si copia/pega este código obtendrá un texto de aspecto difuso. Para evitar esto, simplemente puede aumentar los tamaños de fuente/fotograma, y ​​luego escalar hacia abajo a través del constructor de UIImage. – Sky

0

Puede alojar un UIButton como CustomView dentro de su botón de la barra (o bien establecer el elemento botón de la barra CustomView o puede arrastrar una directamente encima de su UIBarButtonItem), conectarlo como una salida, y luego hacerlo;

-(void) viewDidLoad 
{ 
    //... 
    self.customButton.titleLabel.numberOfLines = 2; 
    self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
    self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter; 
} 

que en mi caso se convierte en

enter image description here

9

Sí se puede. Es bastante simple de hacer. Crea un botón multilínea y úsalo. El "truco" es establecer la propiedad titleLabel numberOfLines para que le gusten las líneas múltiples.

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.titleLabel.numberOfLines = 0; 
[button setTitle:NSLocalizedString(@"Now\nPlaying", nil) forState:UIControlStateNormal]; 
[button sizeToFit]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

Cuando se especifica un tipo de botón de la costumbre que espera configurar todo ... estado seleccionado, etc., que no se muestra aquí para mantener la respuesta enfocado al problema que nos ocupa.

1

enter image description here enter image description here

puedo crear imágenes PNG 2 por mí mismo, y se ve bien.

UIImage *img = [UIImage imageNamed:@"nowplaying.png"]; 
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:@selector(presentNowPlayingMovie)] autorelease]; 
Cuestiones relacionadas