首页 > 解决方案 > 将 UIFont 对象从 React-Native 传递给 Swift

问题描述

我有一个由 React-Native 视图管理的原生 Swift 组件。从我的 RN 视图中,我想传递一个 UIFont 对象,该对象应该可以在 Swift 中使用。通过以下实现,swift 中的属性被调用,但不会从 RN 视图中获取值。

<NativeSwiftView
     uiFontStyle={{ name: "Arial", size: 15 }}
/>

该属性由目标 c 公开,如下所示:

@interface RCT_EXTERN_MODULE(RCTNativeSwiftViewManager, RCTViewManager)
    RCT_EXPORT_VIEW_PROPERTY(uiFontStyle, UIFont)
@end

在 Swift 中,该属性如下所示:

@objc var uiFontStyle = UIFont(name: "", size: 0) {   
    didSet {
        print("Did set: ", uiFontStyle!)
        self.font = uiFontStyle!
    }
  }

在 didSet 运行后,这将打印到控制台:

Did set:  <UICTFont: 0x7fef00c2b2b0> font-family: ".SFUI-Regular"; font-weight: normal; font-style: normal; font-size: 14.00pt

为什么 print 语句的值不是我在 RN 视图中设置的值?

标签: iosreactjsswiftreact-native

解决方案


推荐阅读