Ios 如何在UINavigationBar标题-Swift或Objective-C上设置字距(字符间距)
我已经根据自己的喜好定制了导航栏,但我正在尝试使用Ios 如何在UINavigationBar标题-Swift或Objective-C上设置字距(字符间距),ios,objective-c,swift,user-interface,kerning,Ios,Objective C,Swift,User Interface,Kerning,我已经根据自己的喜好定制了导航栏,但我正在尝试使用NSKernAttributeName增加字距。我正在使用外观代理将导航栏设置为白色文本和自定义字体,但当我尝试添加字距时,它不会生效 [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColo
NSKernAttributeName
增加字距。我正在使用外观代理将导航栏设置为白色文本和自定义字体,但当我尝试添加字距时,它不会生效
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSFontAttributeName,
[NSNumber numberWithFloat:2.0], NSKernAttributeName, nil]];
是否需要执行其他操作来添加一些不太常见的属性,如标题标签的紧排?根据文档,UINavigationBar的
titleTextAttributes
仅允许您指定字体、文本颜色、文本阴影颜色和文本阴影偏移量
如果要使用其他属性,可以使用所需的NSAttributedString
创建UILabel
,并将其设置为控制器的导航项的标题视图
例如:
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的字体、文本颜色、文本阴影颜色和文本阴影偏移量,正如上面@Jesús A.Alvarez所说的
我已经用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
UIViewController扩展
我将上面的答案转换成UIViewController扩展来整理它
Swift 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)
以上答案已更新,适用于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
}
我在我共享的视图控制器子类中重写了setTitle:
,因此您不必到处这样做。使用titleView
的一个不幸的副作用是它似乎会搞乱一些可访问性行为。我很好奇你怎么处理很长的标题。如何防止标题标签覆盖左右导航项按钮?我一直在修剪X字符后的字符串,但它不是很精确:(