[英]Default Font for UINavigationBar Large Title in iOS 11?
I know how to set the color and change the font for the large title in iOS 11, but I am having a hard time finding the default font and font size for the large title.我知道如何在 iOS 11 中为大标题设置颜色和更改字体,但是我很难找到大标题的默认字体和字体大小。 I would like to reproduce the font elsewhere in the app.
我想在应用程序的其他地方重现字体。 I have tried the following but they give no results for font or font size.
我尝试了以下方法,但它们没有给出字体或字体大小的结果。 Not sure where else I should be looking for this.
不知道我应该在哪里寻找这个。
NSLog(@"self.navigationController.navigationBar.largeTitleTextAttributes:%@",self.navigationController.navigationBar.largeTitleTextAttributes);
NSDictionary *dict = self.navigationController.navigationBar.largeTitleTextAttributes;
UIFont *font = [dict objectForKey:@"NSFontAttributeName"];
NSLog(@"font is.. %@, %@, %@",font,font.fontName,font.fontDescriptor);
The proper solution for getting the large title font is the following:获取大标题字体的正确解决方案如下:
Objective-C:目标-C:
UIFont *font = [UIFont preferredFontForTextStyle: UIFontTextStyleLargeTitle];
NSLog("%@", font);
Swift:斯威夫特:
let font = UIFont.preferredFont(forTextStyle: .largeTitle)
print(font)
This will give the correct value even if the user has applied various accessibility and font size changes in the Settings app.即使用户在“设置”应用程序中应用了各种辅助功能和字体大小更改,这也会给出正确的值。
The above will print out the following by default (in a playground):以上将默认打印出以下内容(在操场上):
<UICTFont: 0x7fa865c05390> font-family: ".SFUIDisplay";
<UICTFont: 0x7fa865c05390> font-family: ".SFUIDisplay"; font-weight: normal;
字体粗细:正常; font-style: normal;
字体样式:正常; font-size: 34.00pt
字体大小:34.00pt
Note that the use of .largeTitle
in the code above must be called only with iOS 11. If your app supports iOS 10 or earlier, you must wrap that code in a proper use of @available
.请注意,上面代码中
.largeTitle
的使用只能在 iOS 11 中调用。如果您的应用程序支持 iOS 10 或更早版本,则必须将该代码包装在正确使用@available
。
iOS12 Large Title System Bold 34 (San Francisco) iOS12 大标题系统 Bold 34(旧金山)
[UIFont boldSystemFontOfSize:34]
iOS9 - iOS12 System Semibold 17 (San Francisco) iOS9 - iOS12 System Semibold 17(旧金山)
[UIFont systemFontOfSize:17 weight:UIFontWeightSemibold]
iOS8 System Semibold 17 (Helvetica Neue) iOS8 System Semibold 17 (Helvetica Neue)
[UIFont systemFontOfSize:17 weight:UIFontWeightSemibold]
iOS7 System Bold 17 (Helvetica Neue) iOS7 System Bold 17 (Helvetica Neue)
[UIFont boldSystemFontOfSize:17];
尝试这样的事情:
if #available(iOS 11.0, *) { navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "HelveticaNeue-Bold", size: 20)!] }
On iOS 13 this is the exact font that is used for large titles in navigation bars.在 iOS 13 上,这是用于导航栏中大标题的确切字体。 I compared screenshots pixel by pixel and it matches exactly!
我逐像素比较了屏幕截图,结果完全匹配!
UIFont.systemFont(ofSize: 34, weight: .bold)
I actually found the answer by trying different fonts and sizes.我实际上通过尝试不同的字体和大小找到了答案。 Now, a disclaimer, this may change with accessibility/dynamic font sizes.
现在,免责声明,这可能会随着可访问性/动态字体大小而改变。 But the font and size I found was
但是我发现的字体和大小是
[UIFont systemFontOfSize:32 weight:UIFontWeightBold]
I'd still like to find a way to get this programmatically and will wait to accept an answer in the hopes someone might know how to do that.我仍然想找到一种以编程方式获取此信息的方法,并将等待接受答案,希望有人可能知道如何做到这一点。 In the meantime, hope this will be helpful to someone.
同时,希望这对某人有所帮助。
I'm pretty sure for at least iOS 11+ it is 我非常肯定它至少是iOS 11+
Swift 5: 斯威夫特5:
UIFont.init(name: ".SFUIDisplay-Bold", size: 32)
Easiest way is to use最简单的方法是使用
UIFont.preferredFont(forTextStyle: .largeTitle)
Keep in mind, it's not UINavigationBar's large title, it's a style to use in your own views, that's why the weight is off.请记住,它不是UINavigationBar 的大标题,它是一种在您自己的视图中使用的样式,这就是权重关闭的原因。 But you can apply intended weight to it.
但是您可以对其应用预期的重量。
UIFont(descriptor: normalTitleFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalTitleFont.pointSize)
Full code:完整代码:
let normalTitleFont = UIFont.preferredFont(forTextStyle: .largeTitle)
let largeTitleFont = UIFont(descriptor: normalTitleFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalTitleFont.pointSize)
This approach is better compared to hardcoding sizes or fonts.与硬编码大小或字体相比,这种方法更好。
User can change accessibility settings on device and hardcoded values will be off.用户可以更改设备上的辅助功能设置,硬编码值将关闭。
The value of .largeTitle text style will change as well, and your title will get the the right style. .largeTitle 文本样式的值也会发生变化,您的标题将获得正确的样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.