使用UIAppearance更改标签高度

有没有办法使用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]]; 

在这里输入图像说明

大概你可以在UINavigationBar里获得UILabel的UIAppearance。

我最终通过创build一个自定义的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];