【问题标题】:Setting font and font-size of title bar text in a UITableViewController在 UITableViewController 中设置标题栏文本的字体和字体大小
【发布时间】:2009-10-09 20:47:31
【问题描述】:

我有一个用于 iphone/objective-c 的简单导航应用程序

在推送到视图中的各种 UIViewControllers 中,我可以使用类似的东西在标题栏中设置文本

self.title = @"blah blah blah"

有没有办法控制标题栏文字中标题的fontfont-size

谢谢!

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    调整navcontroller标题文本大小的正确方法是设置navigatorItem的titleView属性

    像这样(在 viewDidLoad 中)

       UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
            tlabel.text=self.navigationItem.title;
        tlabel.textColor=[UIColor whiteColor];
        tlabel.backgroundColor =[UIColor clearColor];
        tlabel.adjustsFontSizeToFitWidth=YES;
        self.navigationItem.titleView=tlabel;
    

    【讨论】:

      【解决方案2】:

      您可能需要对标签进行压印,使其看起来不模糊和扁平:

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          CGRect frame = CGRectMake(0, 0, 400, 44);
          UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
          label.backgroundColor = [UIColor clearColor];
          label.font = [UIFont boldSystemFontOfSize:18.0];
          label.textAlignment = UITextAlignmentCenter;
          label.textColor = [UIColor whiteColor];
          label.text = self.navigationItem.title;
          // emboss so that the label looks OK
          [label setShadowColor:[UIColor darkGrayColor]];
          [label setShadowOffset:CGSizeMake(0, -0.5)];
          self.navigationItem.titleView = label;
      }
      

      【讨论】:

      • 我不知道 FontHelper 是什么,所以我使用了:label.font = [UIFont boldSystemFontOfSize:18.0]; 这似乎非常接近导航栏标题的原始字体。
      • @Ginny - 很抱歉,我没有注意到我离开了我自己的 FontHelper 课程!
      【解决方案3】:

      如果您希望它在 iphone 和 ipad 上都可以使用,并且还希望标题居中,请使用以下代码。

       - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
          label.text=self.navigationItem.title;
          label.textColor=[UIColor whiteColor];
          label.backgroundColor =[UIColor clearColor];
          label.adjustsFontSizeToFitWidth=YES;
          label.font = [AppHelper titleFont];
          label.textAlignment = NSTextAlignmentCenter;
          self.navigationItem.titleView=label;
      }
      

      【讨论】:

        【解决方案4】:

        您可以将任何 UIView 分配给导航控制器的标题区域。

        创建一个UILabel 并根据需要设置其字体和大小,然后将其分配给UIViewController 的navigationItem.titleView 属性。确保 UILabel 的 backgroundColor 设置为 clearColor。

        这仅适用于顶级导航视图。当用户深入查看视图控制器层次结构并显示“后退”按钮时,替代的 titleView 将被忽略并显示常规文本标签。

        【讨论】:

        • 有没有办法为 drill-down 标题栏设置字体大小等?
        • 官方文档说向下钻取标题视图被忽略(可能是因为他们希望用户在文本显示在下一级的“返回”按钮之前看到文本。)但是你总是可以尝试覆盖 viewDidLoad 并递归地向下扫描 NavBar 的视图层次结构,直到找到正确的 UILabel 然后在其上设置字体。
        猜你喜欢
        • 2016-01-30
        • 2014-10-17
        • 2012-03-26
        • 2020-11-01
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多