Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UINavigationBar标题-Swift或Objective-C上设置字距(字符间距)_Ios_Objective C_Swift_User Interface_Kerning - Fatal编程技术网

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字符后的字符串,但它不是很精确:(