我曾尝试在UINavigationController的添加两行标题栏
我想调整字体大小根据串length.My字符串最大尺寸自动设置进入60我已经尝试通过下面的代码来实现
UILabel *bigLabel = [[UILabel alloc] init];
bigLabel.text = @"1234567890 1234567890 1234567890 1234567890 1234567890 123456";
bigLabel.backgroundColor = [UIColor clearColor];
bigLabel.textColor = [UIColor whiteColor];
bigLabel.font = [UIFont boldSystemFontOfSize:20];
bigLabel.adjustsFontSizeToFitWidth = YES;
bigLabel.clipsToBounds = NO;
bigLabel.numberOfLines = 2;
bigLabel.textAlignment = ([self.title length] < 10 ? NSTextAlignmentCenter : NSTextAlignmentLeft);
[bigLabel sizeToFit];
self.navigationItem.titleView = bigLabel;
它并没有为我工作,你可以帮我请。 我对这个做了iPhone和iPad的屏幕
刚刚成立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;
由于@TheTiger对于这样的解决方案。
我已经编辑根据我的要求,他的回答。 所以这里是我的答案
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
NSString *titleStr1 = @"1234567890 1234567890 1234567890 1234567890 123 4568";
NSString *titleStr = @"I am a computer engineer. I wa";
NSLog(@"%d", [titleStr length]);
if([titleStr length]>40 && [titleStr length]<=50){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:13]];
}
else if([titleStr length]>30 && [titleStr length]<=40){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:16]];
}
else if([titleStr length]>50){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:10]];
}
else{
[label setFont:[UIFont boldSystemFontOfSize:20]];
[label setAdjustsFontSizeToFitWidth:YES];
}
[label setText:titleStr];
self.navigationItem.titleView = label;