How to import ttf in React Native "Library"

42 Views Asked by At

hi I was making react native, native view library using create-react-native-library I was trying to make an library that supports the custom ttf file in the project, so that the user can use the font style like this

fontFamily: "the custom font inside the library"

So I wrote this code

import React

@objc(CustomFontViewManager)
class CustomFontViewManager: RCTViewManager {

    @objc func loadFont(_ fontFileName: String) {
        print("loadFont: \(fontFileName)")

        if let fontPath = Bundle.main.path(forResource: fontFileName, ofType: "ttf"),
           let fontData = NSData(contentsOfFile: fontPath),
           let dataProvider = CGDataProvider(data: fontData),
           let fontRef = CGFont(dataProvider) {

            var error: Unmanaged<CFError>?
            if !CTFontManagerRegisterGraphicsFont(fontRef, &error) {
                let errorDescription = CFErrorCopyDescription(error!.takeRetainedValue())
                print("Failed to load font: \(errorDescription)")
            } else {
                print("Succeeded to load font!")
            }
        } else {
            print("Font file not found or unable to load.")
        }

        let fontFamilyNames = UIFont.familyNames

        // 사용 가능한 폰트에 대한 정보 출력
        for familyName in fontFamilyNames {
            print("Font Family: \(familyName)")

            let fontNames = UIFont.fontNames(forFamilyName: familyName)
            for fontName in fontNames {
                print("    Font Name: \(fontName)")
            }
        }
    }

    @objc override static func requiresMainQueueSetup() -> Bool {
        return true
    }
}

however this doesn't work, the code

let fontPath = Bundle.main.path(forResource: fontFileName, ofType: "ttf")

returns "nil"

the project folder looks like this enter image description here

How do I import my ttf file and use as a fontFamily in react native library?

0

There are 0 best solutions below