使用 UIAppearance 更改标签高度

发布于 2024-12-10 05:54:15 字数 671 浏览 0 评论 0原文

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

在此处输入图像描述

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-12-17 05:54:16

我最终通过创建自定义 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];

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;
}

and then setting the titleView property on the navigationItem:

self.navigationItem.titleView = [NavigationBarTitleLabel titleLabelWithTitle:self.title];
涫野音 2024-12-17 05:54:16

也许您可以在 UINavigationBar 中获取 UILabel 的 UIAppearance。

Probably you can get the UIAppearance for UILabel inside UINavigationBar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文