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 ofUILabel. See the example below.Set Left and Right
UIBarButtonItems– you can add them.Also decrease the
FontSizeofLabel. It is 12 in this case.And it will look like this:
For the extended question:
Just make some changes in previous code –
Note: You can’t use
adjustsFontSizeToFitWidth:property ofUILabel, because it doesn’t work forsetNumberOfLines:0in your case you will have to handle it withifcondition.This is method for set
fontSizeOfUILabelaccording to itswidth.