使用UIAppearance更改标签高度 [英] Using UIAppearance to change label height

查看:93
本文介绍了使用UIAppearance更改标签高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to use UIAppearance to change the height of a label inside of a UINavigationBar. Here's the code and an image of what's going on so you can understand what the problem is.

[[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]];

推荐答案

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

I ended up solving this by creating a custom 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属性:

and then setting the titleView property on the navigationItem:

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

这篇关于使用UIAppearance更改标签高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆