iphoneuinavigationcontrollerresizeuinavigationbarautoresize

How to resize Title in a navigation bar dynamically


I have some views that show up in a navigation controller. Two of these views have a longer title for the navigation bar.

The problem is that when the title is too long to fit, some characters are truncated and "..." is added.

Is there any way I can tell the Navigation bar to re-size the title text automatically to fit?


Solution

  • Used the below code in ViewDidload .

    Objective C

    self.title = @"Your TiTle Text";
    UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
    tlabel.text=self.navigationItem.title;
    tlabel.textColor=[UIColor whiteColor];
    tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
    tlabel.backgroundColor =[UIColor clearColor];
    tlabel.adjustsFontSizeToFitWidth=YES;
    tlabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView=tlabel;
    

    Swift Version

    self.title = "Your Title Text"
    let tlabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
    tlabel.text = self.title
    tlabel.textColor = UIColor.white
    tlabel.font = UIFont.systemFont(ofSize: 30, weight: .bold)
    tlabel.backgroundColor = UIColor.clear
    tlabel.adjustsFontSizeToFitWidth = true
    tlabel.textAlignment = .center
    self.navigationItem.titleView = tlabel
    

    Hope it works for you.Thanks