使用自定义字体时,如何垂直校正navigationBar的titleView文本位置?

时间:2011-12-12 13:49:35

标签: iphone ios uilabel uinavigationbar uinavigationitem

我们在导航栏中为titleView使用自定义字体。 不知怎的,Apple总是把这个字体画得太高了。

如何更正在导航栏中使用自定义字体时出现的这种奇怪偏移?

2 个答案:

答案 0 :(得分:23)

我使用了setTitleVerticalPositionAdjustment:forBarMetrics:

兼容性:从iOS 5开始提供。

答案 1 :(得分:13)

您可以将新视图设为titleView,然后为其添加新标签:

UIView * customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 40.0f)];

UILabel * customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 20.0f)];
[customLabel setBackgroundColor:[UIColor clearColor]];
[customLabel setTextColor:[UIColor whiteColor]];
[customLabel setFont:[UIFont systemFontOfSize:12.0f]];
[customLabel setTextAlignment:UITextAlignmentCenter];
[customLabel setText:@"Your Text"];
[customTitleView addSubview:customLabel];
[customLabel release];

[self.navigationItem setTitleView:customTitleView];
[customTitleView release];