据TitleView中文本的长度设置UINavigationBa

据TitleView中文本的长度设置UINavigationBa

本文介绍了如何根据TitleView中文本的长度设置UINavigationBar的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIView *labelView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 255, 40)];
       UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
       [label setTextColor:[UIColor whiteColor]];
        label.lineBreakMode=UILineBreakModeWordWrap;
        label.numberOfLines=0;
        label.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
        label.shadowOffset = CGSizeMake(0.0f, -1.0f);
        label.textAlignment = UITextAlignmentCenter;
        label.adjustsFontSizeToFitWidth=YES;


      [label setBackgroundColor:[UIColor clearColor]];

      switch (genreId) {
       case 1:
        label.text=@"Music";
        self.navigationItem.titleView =label;
        break;
       case 3:
        label.text=@"Family";
        break;
       case 6:
        label.text=@"Literature";
        break;
       case 7:
        label.text=@"Theatre";
        break;
       default:
        label.frame=CGRectMake(0, 0, 255, 40);
        label.text=ArtistName;
        break;
      }


      [label setFont:[UIFont boldSystemFontOfSize:20.0]];
      [labelView  addSubview:label];
      [self.navigationItem setTitleView:labelView];

      [label release];

推荐答案

使用此代码..... chill ......

use this code.....and chill......

NSString *summary;
summary = @" your title";

 // define font size what ever you want....
CGSize s = [summary sizeWithFont:[UIFont systemFontOfSize:30]
               constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)     // - 40 For cell padding
                   lineBreakMode:UILineBreakModeWordWrap];



CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, s.height);
[self.navigationController.navigationBar setFrame:frame];

希望,它将对您有帮助...

Hope, it will help you...

这篇关于如何根据TitleView中文本的长度设置UINavigationBar的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:21