HarmonyOS RN字体设置demo

HarmonyOS
18h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

参考demo:

import {
  ResourceJSBundleProvider,
  RNOHCoreContext,
  RNInstance,
  LogBoxDialog,
  RNOHErrorDialog,
  LogBoxTurboModule,
  ComponentBuilderContext,
  RNComponentContext,
  RNOHContext,
  buildRNComponentForTag,
} from '@rnoh/react-native-openharmony';
import MultiHome from './MultiHome';
import { createRNPackages, LoadManager, buildCustomComponent } from '../rn';
import { ENABLE_CAPI_ARCHITECTURE } from '../rn'
import { arkTsComponentNames } from '../rn/LoadBundle';
import font from '@ohos.font';

const fonts: font.FontOptions[] = [
  {
    familyName: 'Pacifico-Regular',
    familySrc: '/assets/fonts/Pacifico-Regular.ttf'
  },
  {
    familyName: 'StintUltraCondensed-Regular',
    familySrc: '/assets/fonts/StintUltraCondensed-Regular.ttf'
  },
  {
    familyName: 'crn_font_xxx',
    familySrc: '/assets/fonts/crn_font_xxx.ttf'
  }
]

@Entry()
@Component
struct Index {
  @StorageLink('RNOHCoreContext') rnohCoreContext: RNOHCoreContext | undefined = undefined
  @State isBundleReady: boolean = false
  private isMetroAvailable: boolean = false
  private cleanUpCallbacks: (() => void)[] = []
  private logBoxDialogController!: CustomDialogController
  @Builder
  logBoxBuilder(_: ComponentBuilderContext) {
  }

  aboutToAppear() {
    if (!this.rnohCoreContext) return;
    for (const customFont of fonts) {
      font.registerFont(customFont)
    }
    this.loadMetroBundle()
  }

  aboutToDisappear() {
    LoadManager.resetLoadedData()
    this.cleanUpCallbacks.forEach(cleanUp => cleanUp())
  }

  subscribeReload() {
    if (!this.rnohCoreContext) return;
    this.cleanUpCallbacks.forEach(cleanUp => cleanUp())
    this.cleanUpCallbacks.push(this.rnohCoreContext!.devToolsController.eventEmitter.subscribe("RELOAD", async () => {
      this.isBundleReady = false
      this.isMetroAvailable = false
      LoadManager.resetLoadedData()
      this.loadMetroBundle()
    }))
  }

  subscribeLogBox() {
    this.logBoxDialogController = new CustomDialogController({
      cornerRadius: 0,
      customStyle: true,
      alignment: DialogAlignment.TopStart,
      backgroundColor: Color.Transparent,
      builder: LogBoxDialog({
        rnInstance: LoadManager.metroInstance,
        initialProps: {},
        buildCustomComponent: this.logBoxBuilder,
      })
    })

    this.cleanUpCallbacks.push(LoadManager.metroInstance.getTurboModule<LogBoxTurboModule>(LogBoxTurboModule.NAME).eventEmitter.subscribe("SHOW", () => {
      this.logBoxDialogController.open()
    }))
    this.cleanUpCallbacks.push(LoadManager.metroInstance.getTurboModule<LogBoxTurboModule>(LogBoxTurboModule.NAME).eventEmitter.subscribe("HIDE", () => {
      this.logBoxDialogController.close()
    }))
  }

  loadMetroBundle() {
    LoadManager.loadMetroBundle(this.getUIContext()).then((flag: boolean) => {
      console.log('SampleApp --- metro 服务是否可用 = ', JSON.stringify(flag))
      AppStorage.setOrCreate('isMetroAvailable', flag)
      this.isMetroAvailable = flag
      this.register().then((instanceMap: Map<string, RNInstance>) => {
        this.isBundleReady = true;
        console.log("registered instance", JSON.stringify(instanceMap))
      });
    })
  }

  async register(): Promise<Map<string, RNInstance>> {
    if (!this.rnohCoreContext)
      return new Map()

    if (this.isMetroAvailable) {
      this.isBundleReady = true
      this.subscribeReload()
      this.subscribeLogBox()
      return new Map()
    }

    let instanceMap: Map<string, RNInstance> = new Map();
    const cpInstance: RNInstance = await this.rnohCoreContext.createAndRegisterRNInstance({
      createRNPackages: createRNPackages,
      enableNDKTextMeasuring: true,
      enableBackgroundExecutor: true,
      enableCAPIArchitecture: ENABLE_CAPI_ARCHITECTURE,
      arkTsComponentNames: arkTsComponentNames,
      fontOptions: fonts
    });
    const ctxCp: RNComponentContext = new RNComponentContext(
      RNOHContext.fromCoreContext(this.rnohCoreContext!, cpInstance),
      wrapBuilder(buildCustomComponent),
      wrapBuilder(buildRNComponentForTag),
      new Map()
    );
    const bpInstance: RNInstance = await this.rnohCoreContext.createAndRegisterRNInstance({
      createRNPackages: createRNPackages,
      enableNDKTextMeasuring: true,
      enableBackgroundExecutor: true,
      enableCAPIArchitecture: ENABLE_CAPI_ARCHITECTURE,
      arkTsComponentNames: arkTsComponentNames,
      // fontOptions: fonts
    });
    const ctxBp: RNComponentContext = new RNComponentContext(
      RNOHContext.fromCoreContext(this.rnohCoreContext!, bpInstance),
      wrapBuilder(buildCustomComponent),
      wrapBuilder(buildRNComponentForTag),
      new Map()
    );
    LoadManager.cpInstance = cpInstance;
    LoadManager.bpInstance = bpInstance;
    await cpInstance.runJSBundle(new ResourceJSBundleProvider(getContext()
      .resourceManager, 'bundle/basic/basic.harmony.bundle'));
    instanceMap.set('CPReactInstance', cpInstance);
    instanceMap.set('BPReactInstance', bpInstance);
    return instanceMap
  }

  build() {
    Stack() {
      if (this.rnohCoreContext?.isDebugModeEnabled && this.isMetroAvailable) {
        RNOHErrorDialog()
      }
      if (this.isBundleReady) {
        MultiHome()
      }
    }
  }
}
分享
微博
QQ
微信
回复
16h前
相关问题
rn HarmonyOS可运行demo
100浏览 • 1回复 待解决
HarmonyOS 全局设置字体
7浏览 • 1回复 待解决
HarmonyOS RN 项目中使用自定义字体
0浏览 • 0回复 待解决
HarmonyOSRN实现tabview类似的demo
143浏览 • 1回复 待解决
HarmonyOS 如何设置全局字体
180浏览 • 1回复 待解决
HarmonyOS webview字体大小设置
174浏览 • 1回复 待解决
HarmonyOS 全局设置字体没效果
141浏览 • 1回复 待解决
HarmonyOS 字体大小如何设置
874浏览 • 1回复 待解决
HarmonyOS 如何设置RichEditor字体居中
144浏览 • 1回复 待解决
HarmonyOS TabContent怎么设置字体颜色
212浏览 • 1回复 待解决
HarmonyOS 字体行高应该如何设置
187浏览 • 1回复 待解决
HarmonyOS 设置字体有全局方法吗?
449浏览 • 1回复 待解决
HarmonyOS 设置全局字体没有效果
212浏览 • 1回复 待解决
HarmonyOS 如何设置状态字体颜色
139浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
997浏览 • 1回复 待解决
TextInput ,TextArea无法设置字体间距
477浏览 • 1回复 待解决
HarmonyOS 应用内字体大小怎么设置
181浏览 • 1回复 待解决
HarmonyOS RichText组件无法设置字体大小
140浏览 • 1回复 待解决