简体   繁体   English

如何在UINavigationBar标题上设置字距调整(字符间距) - Swift或Objective-C

[英]How to set kerning (spacing between characters) on UINavigationBar title - Swift or Objective-C

I've got my nav bar mostly customized to my liking, but I'm trying to increase the kerning using NSKernAttributeName . 我的导航栏大部分是根据自己的喜好定制的,但我正在尝试使用NSKernAttributeName增加字距。 I'm using the appearance proxy to set the nav bar to white text and a custom font, but when I try to add kerning it doesn't take effect. 我正在使用外观代理将导航​​栏设置为白色文本和自定义字体,但是当我尝试添加字距时,它不会生效。

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

Do I need to do something else to add some of the less common attributes like kerning to the title label? 我是否需要执行其他操作才能在标题标签中添加一些不太常见的属性,如字距调整?

According to the documentation, the titleTextAttributes of UINavigationBar only lets you specify the font, text color, text shadow color, and text shadow offset. 根据文档, UINavigationBartitleTextAttributes仅允许您指定字体,文本颜色,文本阴影颜色和文本阴影偏移。

If you want to use other attributes, you can create a UILabel with the NSAttributedString you want, and set it as the titleView for your controller's navigationItem 如果要使用其他属性,可以使用所需的NSAttributedString创建UILabel ,并将其设置为控制器的navigationItemtitleView

For example: 例如:

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;

I've tried many different ways to accomplish this and found that you can only change the font, text color, text shadow color, and text shadow offset of UINavigationBar as @Jesús A. Alvarez above have said. 我已经尝试了许多不同的方法来实现这一点,并发现你只能更改UINavigationBar的字体,文本颜色,文本阴影颜色和文本阴影偏移量,如上面的@JesúsA。Alvarez所说。

I've converted the code in Swift and it works: 我已经在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

Above answer updated for Swift 4 上面的答案更新为Swift 4

I created a superclass, where I defined this method, which you can call from each subclass you want: 我创建了一个超类,我定义了这个方法,你可以从你想要的每个子类调用它:

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
}

UIViewController Extension UIViewController扩展

I converted the above answers into a UIViewController extension to tidy it away. 我将上面的答案转换为UIViewController扩展,以便将其整理掉。

Swift 3 斯威夫特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
    }
}

Call the extension function from the viewDidLoad() of your view controller. 从视图控制器的viewDidLoad()调用扩展函数。

setUpCustomTitleView(kerning: 2.0) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM