I have try to add a two line title bar in UINavigationController
I want to adjust font size automatically set according to string length.My String max size goes to 60. I have try to implemented through following code
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;
It didn't work for me can you help me please. I have to made this for iPhone and iPad screen
Just set setNumberOfLines:
0 of UILabel
. See the example below.
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;
Set Left and Right UIBarButtonItems
- you can add them.
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;
Also decrease the FontSize
of Label
. It is 12 in this case.
And it will look like this:
For the extended question:
Just make some changes in previous code -
[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];
Note: You can't use adjustsFontSizeToFitWidth:
property of UILabel
, because it doesn't work for setNumberOfLines:0
in your case you will have to handle it with if
condition.
This is method for set fontSize
Of UILabel
according to its width
.
[label setNumberOfLines:1];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 1.0;
thanks to @TheTiger for such a solution.
I have edited his answer according my requirement. so here is my answer
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;