9

“正在播放”在 UIBarButtonItem 的一行中。我必须把它放在两行中,比如“Now”在顶部,“Playing”在底部。我编写了以下代码行:-

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]

                              initWithTitle:@"Now Playing" 

                              style:UIBarButtonItemStyleBordered 

                              target:self 

                              action:@selector(flipView)];


    self.navigationItem.rightBarButtonItem = flipButton;  

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

4

4 回答 4

10

是的你可以。做起来相当简单。创建一个多行按钮并使用它。“诀窍”是设置 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];

当您指定自定义按钮类型时,它希望您配置所有内容...选定状态等,此处未显示以使答案集中在手头的问题上。

于 2013-10-17T02:54:36.227 回答
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];
}
于 2012-03-07T13:08:13.593 回答
0

您可以在条形按钮内将 UIButton 作为 customView 托管(设置条形按钮项 customView 或者您可以直接在 UIBarButtonItem 顶部拖动一个),将其作为插座连接,然后执行;

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

在我的情况下变成

在此处输入图像描述

于 2013-01-15T09:13:35.380 回答
0

在此处输入图像描述 在此处输入图像描述

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

UIImage *img = [UIImage imageNamed:@"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:@selector(presentNowPlayingMovie)] autorelease];
于 2013-11-21T14:17:20.787 回答