UIBarButtonItem宽度带有两行标签

时间:2013-04-01 10:25:57

标签: iphone ios objective-c uinavigationbar uibarbuttonitem

我想知道如何从UIBarButtonItem中的双线标签左侧和右侧去掉空格,其标签由setCustomView设置,如下所示:

UIButton* forwardButton = [UIButton buttonWithType:101];

[forwardButton setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];
[forwardButton.titleLabel setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];

[forwardButton setTitle:@"Now\nPlaying" forState:UIControlStateNormal];
[forwardButton addTarget:self action:@selector(openPlayer) forControlEvents:UIControlEventTouchUpInside];

[forwardButton.titleLabel setNumberOfLines:2];
[forwardButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[forwardButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[forwardButton.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];

[navButton setCustomView:forwardButton];

Here是我得到的结果。

按钮的宽度就像标签的文字写在this之类的一行中一样。

2 个答案:

答案 0 :(得分:1)

您可以尝试更改按钮的界限。

UIButton* forwardButton = [UIButton buttonWithType:101];
[forwardButton setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];
[forwardButton.titleLabel setTransform:CGAffineTransformMakeScale(-1.0, 1.0)];

[forwardButton setTitle:@"Now\nPlaying" forState:UIControlStateNormal];
[forwardButton.titleLabel setNumberOfLines:2];
[forwardButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[forwardButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[forwardButton.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];

forwardButton.bounds = CGRectMake(0, 0, 30, 30);


UIBarButtonItem* forwardButtonItem = [[UIBarButtonItem alloc] initWithCustomView: forwardButton];
self.navigationItem.rightBarButtonItem = forwardButtonItem;

测试了这个并且正常工作

答案 1 :(得分:0)

NSString   *titleStr=@"Now\Playing";
BarButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];

[BarButton setTitle:titleStr forState:UIControlStateNormal];