[英]UIBarButtonItem width with a two-line label
I would like to know how can I get rid of spaces to the left and right from a two-line label in UIBarButtonItem, which has its view set by setCustomView as follows: 我想知道如何从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 is the result I get. 这是我得到的结果。
The width of the button is as if the label had text written in one line like this . 按钮的宽度是因为如果标签已经在文字像一条线写这个 。
You could try changing the bounds for the button. 您可以尝试更改按钮的边界。
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;
tested this and works 测试这个并工作
NSString *titleStr=@"Now\Playing";
BarButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[BarButton setTitle:titleStr forState:UIControlStateNormal];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.