我的导航栏主要是根据我的喜好定制的,但我正在尝试使用NSKernAttributeName
来增加字距调整。我正在使用外观代理将导航栏设置为白色文本和自定义字体,但当我尝试添加字距调整时,它不会生效。
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSFontAttributeName,
[NSNumber numberWithFloat:2.0], NSKernAttributeName, nil]];
我是否需要做一些其他的事情来添加一些不太常见的属性,比如字距调整到标题标签?
发布于 2013-12-01 18:59:09
根据文档,UINavigationBar
的titleTextAttributes
只允许您指定字体、文本颜色、文本阴影颜色和文本阴影偏移。
如果要使用其他属性,可以使用所需的NSAttributedString
创建一个UILabel
,并将其设置为控制器的navigationItem
的titleView
例如:
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;
发布于 2015-11-17 08:16:53
我尝试了许多不同的方法来实现这一点,我发现你只能改变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
https://stackoverflow.com/questions/20310929
复制相似问题