只需设置setNumberOfLines:
0的UILabel
. 请参见下面的示例。
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setText:@"1234567890 1234567890 1234567890 1234567890 1234567890 123456"];
self.navigationItem.titleView = label;
设置左右UIBarButtonItems
- 您可以添加它们。
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(leftPress:)];
self.navigationItem.leftBarButtonItem = leftBarButton;
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightPress:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
也减少FontSize
。在这种情况下Label
是12 。它看起来像这样:
对于扩展问题:
只需对以前的代码进行一些更改 -
[label setNumberOfLines:2]; //Set it 2 instead of 0
NSString *titleStr = @"3456456676456464554541223434484384233456744444444456785643367";
//Check your string length then set font according to it.
//I have set font size according to your requirement
//which is 0-60.
if([titleStr length]>40 && [titleStr length]<=52){
[label setFont:[UIFont fontWithName:@"Arial" size:13.0]];
}
else if([titleStr length]>52){
[label setFont:[UIFont fontWithName:@"Arial" size:11.5]];
}
[label setText:titleStr];
注意:您不能使用adjustsFontSizeToFitWidth:
的属性UILabel
,因为它不适用于setNumberOfLines:0
您的情况,您必须有条件地处理它if
。
这是根据其设置fontSize
的方法。UILabel
width
[label setNumberOfLines:1];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 1.0;