Bläddra i källkod

optimize font loading in DIgiXLite

alqindiirsyam 2 år sedan
förälder
incheckning
96588666d9

BIN
appbuilder-ios/DigiXLite/DigiXLite.xcworkspace/xcuserdata/akhmadalqindiirsyam.xcuserdatad/UserInterfaceState.xcuserstate


+ 34 - 26
appbuilder-ios/DigiXLite/DigiXLite/Source/DigiX.swift

@@ -1503,19 +1503,19 @@ extension UIFont {
     static var isOverrided: Bool = false
     static let FONT_SELECT = 0
 
-    @objc class func libSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.regular)
-        return UIFont(name: LibFontName.regular, size: size)!
+        return UIFont(name: LibFontName.regular, size: size)
     }
 
-    @objc class func libBoldSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libBoldSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.bold)
-        return UIFont(name: LibFontName.bold, size: size)!
+        return UIFont(name: LibFontName.bold, size: size)
     }
 
-    @objc class func libItalicSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libItalicSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.italic)
-        return UIFont(name: LibFontName.italic, size: size)!
+        return UIFont(name: LibFontName.italic, size: size)
     }
 
     @objc convenience init(myCoder aDecoder: NSCoder) {
@@ -1568,29 +1568,37 @@ extension UIFont {
     
     class func jbs_registerFont(withFilenameString filenameString: String) {
 
-        guard let pathForResourceString = Bundle.resourceBundle(for: DigiX.self).path(forResource: filenameString, ofType: "ttf") else { //resourcesMediaBundle
-            //print(("UIFont+:  Failed to register font - path for resource not found.")
-            return
-        }
-
-        guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
-            //print(("UIFont+:  Failed to register font - font data could not be loaded.")
-            return
-        }
-
-        guard let dataProvider = CGDataProvider(data: fontData) else {
-            //print(("UIFont+:  Failed to register font - data provider could not be loaded.")
-            return
-        }
-
-        guard let font = CGFont(dataProvider) else {
-            //print(("UIFont+:  Failed to register font - font could not be loaded.")
+//        guard let pathForResourceString = Bundle.resourceBundle(for: Nexilis.self).path(forResource: filenameString, ofType: "ttf") else { //resourcesMediaBundle
+//            //print("UIFont+:  Failed to register font - path for resource not found.")
+//            return
+//        }
+        
+        guard let pathForResourceURL = Bundle.resourceBundle(for: DigiX.self).url(forResource: filenameString, withExtension: "ttf") else { //resourcesMediaBundle
+            //print("UIFont+:  Failed to register font - path for resource not found.")
             return
         }
-
+        
         var errorRef: Unmanaged<CFError>? = nil
-        if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) {
-        }
+        CTFontManagerRegisterFontsForURL(pathForResourceURL as CFURL, .process, &errorRef)
+
+//        guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
+//            //print("UIFont+:  Failed to register font - font data could not be loaded.")
+//            return
+//        }
+//
+//        guard let dataProvider = CGDataProvider(data: fontData) else {
+//            //print("UIFont+:  Failed to register font - data provider could not be loaded.")
+//            return
+//        }
+//
+//        guard let font = CGFont(dataProvider) else {
+//            //print("UIFont+:  Failed to register font - font could not be loaded.")
+//            return
+//        }
+//
+//        var errorRef: Unmanaged<CFError>? = nil
+//        if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) {
+//        }
     }
 }