使用titleTextAttributes时,UINavigationbar标题被截断

时间:2011-12-25 05:56:57

标签: uinavigationcontroller

我的iPhone应用程序有一个标签栏控制器,在每个标签上(4个标签)有一个UINavigationController,我想要一个不同的颜色并更改标题上的字体,所以我自己制作了自己的定制导航控制器所以这些变化只有一个地方。色调很简单,工作正常,但是当我尝试使用setTitleTextAttributes设置字体时,它会更改字体,但在某些视图中,标题显示最后被截断(即“我的标题...”)。如果我更改视图然后返回带有截断标题的视图,标题确实会正确显示。

目前我的自定义UINavigationController中的viewDidLoad有:

UIFont *font = [UIFont fontWithName:@"GillSans-Bold" size:22];
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys:font, UITextAttributeFont, nil];
[self.navigationBar setTitleTextAttributes:attr];

我的想法是它正在改变titleView的字体,但它没有根据新的大小调整大小(因为它是一个更大的字体)。另一个问题是,当手机转向横向时,任何挂低(g,p,y)的字母都会将底部切断。有没有办法调整标签大小或允许设置minimumFontSize,以便文本在太大时缩小?

3 个答案:

答案 0 :(得分:3)

这对我有用

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setTitle:self.station.title];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
    label.backgroundColor = [UIColor clearColor];
    [label setFont:[UIFont fontWithName:@"GillSans-Bold" size:24.0]];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.text = self.title;    
    self.navigationItem.titleView = label;
}

答案 1 :(得分:0)

我认为这个问题是w.r.t rootviewcontroller。标签栏是在根视图控制器中定义的吗?

如果你通过导航控制器推送和弹出,那么无论导航如何,所有内容都是相同的。

答案 2 :(得分:0)

这对我有用:

+ (void)setNavBarTitle:(NSString *)title forUINavigationItem:(UINavigationItem*)navigationItem
{
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:24];
    label.textAlignment = UITextAlignmentCenter;
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.text = title;
    [label sizeToFit];
    navigationItem.titleView = label;
}
相关问题