使用UIAppearance更改标签高度

时间:2011-10-15 16:45:15

标签: iphone objective-c ios uinavigationbar ios5

有没有办法使用UIAppearance来改变UINavigationBar内部标签的高度。这是代码和正在发生的事情的图像,以便您了解问题所在。

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsLandscapePhone];

NSDictionary *textAttributes =  [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIFont fontWithName:@"MarketingScript" size:34.0], nil] 
                                                            forKeys:[NSArray arrayWithObjects:UITextAttributeFont, nil]];

enter image description here

2 个答案:

答案 0 :(得分:1)

我最终通过创建自定义UIView解决了这个问题:

+(NavigationBarTitleLabel *)titleLabelWithTitle:(NSString *)title {
  // this will appear as the title in the navigation bar
  NavigationBarTitleLabel *labelWrapper = [[NavigationBarTitleLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 34)];
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, 200, 32)];

  label.font            = [UIFont fontWithName:@"MarketingScript" size:28.0];
  label.text            = title;
  label.textColor       = XHEXCOLOR(0XFFFFFF);
  label.textAlignment   = UITextAlignmentCenter;
  label.backgroundColor = [UIColor clearColor];
  label.layer.shadowColor = [UIColor blackColor].CGColor;
  label.layer.shadowRadius = 2;
  [labelWrapper addSubview:label];
  [labelWrapper sizeToFit];

  return labelWrapper;
}

然后在navigationItem上设置titleView属性:

self.navigationItem.titleView = [NavigationBarTitleLabel titleLabelWithTitle:self.title];

答案 1 :(得分:0)

你可以在UINavigationBar中获得UILabel的UIAppearance。