【问题标题】:Using UIAppearance to change label height使用 UIAppearance 改变标签高度
【发布时间】:2011-12-08 09:57:57
【问题描述】:

有什么方法可以使用 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]];

【问题讨论】:

    标签: iphone objective-c ios uinavigationbar ios5


    【解决方案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];
    

    【讨论】:

    • 如果您只是向其中添加 UILabel,在这种情况下使用自定义视图有什么好处?为什么不把它做成一个简单的 UIView?
    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2017-04-07
    相关资源
    最近更新 更多