简体   繁体   English

带有两行标签的UIBarButtonItem宽度

[英]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.

相关问题 获取UIBarButtonItem的宽度 - Get the width of a UIBarButtonItem 如何知道标签最后一行的宽度? - How to know the width of last line of label? UIBarButtonItem外观setBackgroundImage按钮下的黑线 - UIBarButtonItem appearance setBackgroundImage black line under buttons 如何调整rightBarButtonItems中两个UIBarButtonItem之间的空间 - How to adjust space between two UIBarButtonItem in rightBarButtonItems 如何在一个UIBarButtonItem中使用两个函数? - How to use two functions in one UIBarButtonItem? 如何在不使用图像的情况下以编程方式将自定义宽度的UIBarButtonItem添加到UINavigationItem - How to add a UIBarButtonItem of custom width to a UINavigationItem programmatically without using an image 我应该将UIBarButtonItem设置为什么宽度来制作方形按钮? - What width should I set a UIBarButtonItem to make a square button? 将具有两个UIBarButtonItem的UIToolbar添加到UINavigationBar:可怜的UIToolbar和iPhone 4怎么样 - Adding UIToolbar with two UIBarButtonItem to a UINavigationBar: poor UIToolbar and what about iPhone 4 如何使UIBarButtonItem的文本换成两行? - How can I make the text of a UIBarButtonItem wrap to two lines? UINavigationItem标题标签设置宽度? - UINavigationItem title label set width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM