UINavigationBar title label text

前端 未结 3 1732
情歌与酒
情歌与酒 2021-01-12 19:40

Is it possible to get the title text to shrink to fit in the UINavigationBar in iOS.

(for portrait iPhone app with no autolayout).

I\'m setting

3条回答
  •  终归单人心
    2021-01-12 19:57

    You can create your own title view to make it.

    Something like this:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
      //Do any additional setup after loading the view, typically from a nib.
         UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; //<<---- Actually will be auto-resized according to frame of navigation bar;
        [titleLabelView setBackgroundColor:[UIColor clearColor]];
        [titleLabelView setTextAlignment: NSTextAlignmentCenter];
        [titleLabelView setTextColor:[UIColor whiteColor]];
        [titleLabelView setFont:[UIFont systemFontOfSize: 27]]; //<<--- Greatest font size
        [titleLabelView setAdjustsFontSizeToFitWidth:YES]; //<<---- Allow shrink
         // [titleLabelView setAdjustsLetterSpacingToFitWidth:YES];  //<<-- Another option for iOS 6+
        titleLabelView.text = @"This is a Title";
    
        navigationBar.topItem.titleView = titleLabelView;
    
        //....
    }
    

    Hope this helps.

提交回复
热议问题