0

我想知道如何从 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];

是我得到的结果。

按钮的宽度就好像标签有这样的一行文字

4

2 回答 2

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;

测试了这个并且有效

于 2013-04-01T10:37:11.500 回答
0
NSString   *titleStr=@"Now\Playing";
BarButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];

[BarButton setTitle:titleStr forState:UIControlStateNormal];
于 2013-04-01T10:43:39.257 回答