本文介绍了React Native iOS NavigatorIOS 标题字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 React Native,我想改变 NavigatorIOS 标题的字体,我发现了一些有希望的链接:Github 问题堆栈溢出.然而遗憾的是,两者都没有帮助.

I am currently developing a React Native and I want to change the font of the NavigatorIOS title, I've found a few promising links from: Github issue and Stack Overflow. However sadly neither have helped.

我目前在 AppDelegate.m 中有此代码

I currently have this code in my AppDelegate.m

[[UINavigationBar appearance] setTitleTextAttributes:@{
 NSFontAttributeName : [UIFont fontWithName:@"Avenir-Light" size:22.0],
 NSForegroundColorAttributeName : [UIColor whiteColor]
}];

这也不会改变 NavigatorIOS 在开始时提供的字体的标题栏字体.

This also doesn't change the font of the title bar from the font the NavigatorIOS is given at the start.

推荐答案

我不相信 Rahul 由于 React Native 编码导航栏外观变化的方式,将起作用.

I don't believe the answer posted by Rahul will work due to the way React Native codes the Navigation Bar appearance changes.

我做了一个小补丁,可以让你的代码工作.我可能会将此提交给 React Native,但尚未决定:

I made a tiny patch that would allow your code to work. I may submit this to React Native, but haven't decided yet:

--- a/node_modules/react-native/React/Views/RCTWrapperViewController.m
+++ b/node_modules/react-native/React/Views/RCTWrapperViewController.m
@@ -115,9 +115,11 @@ static UIView *RCTFindNavBarShadowViewInView(UIView *view)
     bar.barTintColor = _navItem.barTintColor;
     bar.tintColor = _navItem.tintColor;
     bar.translucent = _navItem.translucent;
-    bar.titleTextAttributes = _navItem.titleTextColor ? @{
-      NSForegroundColorAttributeName: _navItem.titleTextColor
-    } : nil;
+    if (_navItem.titleTextColor != nil) {
+        NSMutableDictionary *newAttributes = bar.titleTextAttributes ? bar.titleTextAttributes.mutableCopy :  [NSMutableDictionary new];
+        [newAttributes setObject:_navItem.titleTextColor forKey:NSForegroundColorAttributeName];
+        bar.titleTextAttributes = newAttributes;
+    }

     RCTFindNavBarShadowViewInView(bar).hidden = _navItem.shadowHidden;

使用此补丁,您应该能够执行以下操作 (Swift):

With this patch, you should be able to do the following (Swift):

 UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name:"Avenir-Light", size: 22.0) as Any]

这篇关于React Native iOS NavigatorIOS 标题字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:32