如何在导航栏中的UIBarButtonItem中放置两行文本?

9
"

“Now Playing”在UIBarButtonItem中只有一行。我需要将它分成两行,就像“Now”在顶部,“Playing”在底部一样。我已经编写了以下代码:

"
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]

                              initWithTitle:@"Now Playing" 

                              style:UIBarButtonItemStyleBordered 

                              target:self 

                              action:@selector(flipView)];


    self.navigationItem.rightBarButtonItem = flipButton;  

我想在“正在播放”之间插入换行符。请帮我实现。


抱歉,它没有起作用。 - user1053588
好的。看起来这是一个重复的问题。但似乎没有简单的方法... http://stackoverflow.com/questions/2614098/how-can-i-make-the-text-of-a-uibarbuttonitem-wrap-to-two-lines - barley
4个回答

9

是的,你可以这样做。操作相当简单,只需创建一个多行按钮并使用它即可。 "诀窍"在于设置titleLabel numberOfLines属性,以便它支持多行。

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];

当你指定一个自定义的按钮类型时,它期望你配置一切...选择状态等等,这里没有展示出来,以保持答案集中于手头的问题。


4
- (void)createCustomRightBarButton_
{
    UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
    addCustomLabel.text =@"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];
}

我发现textColor被忽略了,导航项始终应用默认的蓝色。如果要更改颜色,请设置UIBarButtonItem的tintColor。 - Sky
特别是在较新的设备上,如果您复制/粘贴此代码,则会得到模糊的文本。为了解决这个问题,您可以简单地增加字体/框架大小,然后通过UIImage的构造函数进行缩小。 - Sky

0

你可以将一个 UIButton 作为自定义视图托管在你的按钮栏内(无论是设置按钮栏项 customView 还是直接将其拖放到 UIBarButtonItem 上面),将其挂钩为输出口,然后执行以下操作:

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

在我的情况下变成了

enter image description here


0

enter image description here enter image description here

我自己创建了2个PNG图像,看起来很不错。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接