简体   繁体   English

导航栏标题-更改字体而不更改大小

[英]Navigation Bar Title - Change Font without changing size

I'm having trouble to change both the regular and large titles of my Navigation Bar. 我无法同时更改导航栏的常规标题和大标题。 I only want to change the font, without interfering on the font size. 我只想更改字体,而不会干扰字体大小。 I tried getting the pointSize, but it's not available for NavBar titles. 我尝试获取pointSize,但不适用于NavBar标题。 How can I do it? 我该怎么做?

There is no way to change only font size. 无法更改字体大小。 Since the font represents both size and the font itself. 由于字体代表大小和字体本身。

Solution : change the font (both size and font) but set the default size. 解决方案 :更改字体(大小和字体),但设置默认大小。 If you haven't made any changes to your project regarding the size then it's 17 by default. 如果您尚未对项目进行任何大小更改,则默认为17 I tested it out and found it out. 我对其进行了测试并找到了。

So here is what you can do: 因此,您可以执行以下操作:

let attributes: [NSAttributedStringKey: Any] = [
        NSAttributedStringKey.font: YourFontOfSize(size: 17.0)
    ]

navigationController?.navigationBar.titleTextAttributes = attributes

Hope it helps! 希望能帮助到你! Please let me know if it worked. 请让我知道它是否有效。

UPDATE: Navigation bar also has a property called .largeTitleTextAttributes . 更新:导航栏还具有一个名为.largeTitleTextAttributes的属性。 So you can do the same as I did for .titleTextAttributes : Find out the default size by just trying to set the system font with sizes: 15, 16, 17 etc. And then when the size matches you will know which is the default size. 因此,您可以执行与.titleTextAttributes相同的.titleTextAttributes :通过尝试设置大小为.titleTextAttributes的系统字体来找出默认大小。然后,当大小匹配时,您将知道哪个是默认大小。 And then apply your custom font using the default size that you found out. 然后使用发现的默认大小应用自定义字体。

Well There is a way but not programmatically. 嗯,有一种方法,但是不是以编程方式。 You can manually set the different font sizes from the utility panes in attribute inspector. 您可以从属性检查器的实用程序窗格中手动设置不同的字体大小。

] [

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

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