React Native iOS NavigatorIOS 标题字体

Posted

技术标签:

【中文标题】React Native iOS NavigatorIOS 标题字体【英文标题】:React Native iOS NavigatorIOS title font 【发布时间】:2016-01-23 14:03:59 【问题描述】:

我目前正在开发一个 React Native 并且我想更改 Navigatorios 标题的字体,我从Github issue 和Stack Overflow 找到了一些有希望的链接。然而遗憾的是,两者都没有帮助。

我目前在我的 AppDelegate.m 中有这段代码

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

这也不会改变标题栏的字体,而不是 NavigatorIOS 在开始时给出的字体。

【问题讨论】:

你能解决这个问题吗? 【参考方案1】:

你把这段代码放在AppDelegate哪里。

最好放入一个RCT_EXPORT_METHORD,它应该可以从任何 js 类中调用。

在 AppDelagete.h 中

import BridgeModule.h
Add RCTBridgeModule protocol

在 AppDelegate.m 中

Add:  RCT_EXPORT_MODULE() below implementation
@implementation AppDelegate
RCT_EXPORT_MODULE()

添加这个新功能

RCT_EXPORT_METHOD(updateNavigationBar)

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

];


js 的变化

添加以下行

var AppDelegate = require('NativeModules').AppDelegate;

someJSFunction

    AppDelegate.updateNavigationBar();


这将有效。 (此处输入的代码)

【讨论】:

【参考方案2】:

我不相信 Rahul 发布的答案会起作用,因为 React Native 编码导航栏外观的方式发生了变化。

我制作了一个小补丁,可以让您的代码正常工作。我可能会将此提交给 React Native,但尚未决定:

--- 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):

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

【讨论】:

以上是关于React Native iOS NavigatorIOS 标题字体的主要内容,如果未能解决你的问题,请参考以下文章

react-native 配置启动图支持ios和android

React Native Push Notification - react-native-fcm - ios无法构建

在 iOS 中将 react-native-navigation 与 react-native-callkit 集成

React Native,AwesomeProject,react-native run-ios:** 构建失败 **

React Native iOS原生模块开发实战|教程|心得|如何创建React Native iOS原生模块

react-native: ios原生调用js方法