Using UIAppearance to change label height

我们两清 提交于 2020-01-11 06:53:05

问题


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


回答1:


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



回答2:


Probably you can get the UIAppearance for UILabel inside UINavigationBar.



来源:https://stackoverflow.com/questions/7779190/using-uiappearance-to-change-label-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!