【问题标题】:How to set kerning (spacing between characters) on UINavigationBar title - Swift or Objective-C如何在 UINavigationBar 标题上设置字距调整(字符之间的间距) - Swift 或 Objective-C
【发布时间】:2013-12-17 03:15:46
【问题描述】:

我的导航栏大部分是根据自己的喜好定制的,但我正在尝试使用NSKernAttributeName 来增加字距调整。我正在使用外观代理将导航​​栏设置为白色文本和自定义字体,但是当我尝试添加字距调整时,它不会生效。

[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIColor whiteColor], NSForegroundColorAttributeName,
                                                     [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSFontAttributeName,
                                                     [NSNumber numberWithFloat:2.0], NSKernAttributeName, nil]];

我是否需要做一些其他事情来为标题标签添加一些不太常见的属性,例如字距调整?

【问题讨论】:

    标签: ios objective-c swift user-interface kerning


    【解决方案1】:

    根据文档,UINavigationBartitleTextAttributes 只允许您指定字体、文本颜色、文本阴影颜色和文本阴影偏移量。

    如果你想使用其他属性,你可以用你想要的NSAttributedString创建一个UILabel,并将它设置为你控制器的titleViewtitleView

    例如:

    UILabel *titleLabel = [UILabel new];
    NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                 NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
                                 NSKernAttributeName: @2};
    titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.navigationItem.title attributes:attributes];
    [titleLabel sizeToFit];
    self.navigationItem.titleView = titleLabel;
    

    【讨论】:

    • 我在我共享的视图控制器子类中覆盖了setTitle:,因此您不必在任何地方都这样做。
    • 使用titleView 的一个不幸的副作用是它似乎会弄乱一些可访问性行为。 stackoverflow.com/questions/36990907/…
    【解决方案2】:

    我尝试了很多不同的方法来实现这一点,发现您只能像上面@Jesús A. Alvarez 所说的那样更改 UINavigationBar 的字体、文本颜色、文本阴影颜色和文本阴影偏移量。

    我已经在 Swift 中转换了代码并且可以正常工作:

    let titleLabel = UILabel()
    
    let attributes: NSDictionary = [
        NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20),
        NSForegroundColorAttributeName:UIColor.whiteColor(),
        NSKernAttributeName:CGFloat(2.0)
    ]
    
    let attributedTitle = NSAttributedString(string: "UINavigationBar Title", attributes: attributes as? [String : AnyObject])
    
    titleLabel.attributedText = attributedTitle
    titleLabel.sizeToFit()
    self.navigationItem.titleView = titleLabel
    

    【讨论】:

      【解决方案3】:

      以上答案已针对 Swift 4 更新

      我创建了一个超类,我在其中定义了这个方法,你可以从你想要的每个子类中调用它:

      func setNavigationTitle(_ title: String, kern: CGFloat) {
          let titleLabel = UILabel()
      
          let attributes = [
              NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 18),
              NSAttributedStringKey.foregroundColor: UIColor.white,
              NSAttributedStringKey.kern: kern] as [NSAttributedStringKey : Any]
      
          let attributedTitle = NSAttributedString(string: title, attributes: attributes)
      
          titleLabel.attributedText = attributedTitle
          titleLabel.sizeToFit()
          self.navigationItem.titleView = titleLabel
      }
      

      【讨论】:

      • 我很好奇您如何处理很长的标题。你如何防止你的titleLabel 覆盖左右导航项栏按钮?我一直在 X 字符后修剪字符串,但不是很精确:(
      【解决方案4】:

      UIViewController 扩展

      我把上面的答案转换成 UIViewController 扩展来整理一下。

      斯威夫特 3

      extension UIViewController {
          func setUpCustomTitleView(kerning: CGFloat) {
      
              let titleLabel = UILabel()
      
              guard let customFont = UIFont(name: "Montserrat-SemiBold", size: 18) else { return }
      
              let attributes = [NSForegroundColorAttributeName: UIColor.gray,
                            NSFontAttributeName: customFont,
                            NSKernAttributeName: kerning] as [String : Any]
      
              guard let title = title else { return }
      
              let attributedTitle = NSAttributedString(string: title, attributes: attributes)
      
              titleLabel.attributedText = attributedTitle
              titleLabel.sizeToFit()
              navigationItem.titleView = titleLabel
          }
      }
      

      从视图控制器的 viewDidLoad() 调用扩展函数。

      setUpCustomTitleView(kerning: 2.0) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-29
        • 1970-01-01
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        相关资源
        最近更新 更多