【问题标题】:Setting navigationBar title font of the next view? Help with "setTitleView" to some label?设置下一个视图的导航栏标题字体?帮助“setTitleView”到某个标签?
【发布时间】:2011-10-30 17:48:50
【问题描述】:

我正在尝试设置我的导航栏的标题字体,所以我可以更改字体大小,因为我想要一个更长的标题...我这样做:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *navTitle = [[[UILabel alloc] init] autorelease];
        [navTitle sizeToFit];
        [navTitle setFont:[UIFont systemFontOfSize:30]];
        [navTitle adjustsFontSizeToFitWidth];
        [navTitle setBackgroundColor:[UIColor clearColor]];
        [navTitle setTextColor:[UIColor whiteColor]];
        [navTitle setTextAlignment:UITextAlignmentCenter];
        [navTitle setText:@"SuperLongTitleOfMyNaviBar"];
        [self.navigationItem setTitleView:navTitle];

    }
    return self;
}

标题应该是空的地方...有什么建议吗?

提前非常感谢!

【问题讨论】:

  • 为什么需要这个 UILabel?您只需直接将您的字符串写入标题视图即可。
  • 因为它不适合 :) 它太长了......这样,标签会自动调整文本大小以适合
  • 你能发setting text alignment, color, bg color, etc. here...代码吗?
  • 在这种情况下,您可能希望在详细信息 VC 中执行此操作。查看本教程 - 非常有帮助:icab.de/blog/2009/10/13/…
  • @Kashiv 和其他人,我刚刚完全重新编辑了我的问题,使其更加清晰和普遍......

标签: iphone ios uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

在您的代码中,在您调用sizeToFit 时,标签没有text。所以frame 不会改变。

将text 分配给标签后,您应该调用sizeToFit 方法。

【讨论】:

  • 如果你想让adjustsFontSizeToFitWidth工作,你必须明确设置标签的frame。
  • 不...如果我做了naviTitle.adjustFontSizeToFitWidth = YES; 奇怪... :) 无论如何都很好,谢谢
  • 是的。 [label adjustsFontSizeForWidth] 是一个 getter 方法。要设置值,您应该使用[label setAdjustsFontSizeForWidth:YES]
【解决方案2】:

您忘记设置标签的框架 - 替换

UILabel *navTitle = [[[UILabel alloc] init] autorelease];

与

UILabel *navTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 32)] autorelease];

您可以将CGRectMake(0, 0, 260, 32) 替换为您的值。

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2012-09-02
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多