如何在UINavigationBar标题上设置字距调整(字符间距) - Swift或Objective-C [英] How to set kerning (spacing between characters) on UINavigationBar title - Swift or Objective-C
问题描述
我的导航栏大部分是根据自己的喜好自定义的,但我正在尝试使用 NSKernAttributeName
来增加字距。我正在使用外观代理将导航栏设置为白色文本和自定义字体,但是当我尝试添加字距时,它不会生效。
I've got my nav bar mostly customized to my liking, but I'm trying to increase the kerning using 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?
推荐答案
根据文档, titleTextAttributes
UINavigationBar
只允许您指定字体,文本颜色,文本阴影颜色和文本阴影偏移。
According to the documentation, the titleTextAttributes
of UINavigationBar
only lets you specify the font, text color, text shadow color, and text shadow offset.
如果您想使用其他属性,您可以使用所需的 NSAttributedString
创建 UILabel
,并将其设置为 titleView
为您的控制器的 navigationItem
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
例如:
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;
这篇关于如何在UINavigationBar标题上设置字距调整(字符间距) - Swift或Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!