Download font .ttf file from web and store on iPhone












3















Is it possible to download .ttf file from web and store it on iPhone. Then use that for for labels and all other stuff ? Because my client want to control fonts from database and don't want to just drop fonts to xcode project right away.



So in future if he wants to change font, he will add new font to database, app will recognize new font on web (thats already done with images, not a problem), download it and use as font.



Thanks.










share|improve this question



























    3















    Is it possible to download .ttf file from web and store it on iPhone. Then use that for for labels and all other stuff ? Because my client want to control fonts from database and don't want to just drop fonts to xcode project right away.



    So in future if he wants to change font, he will add new font to database, app will recognize new font on web (thats already done with images, not a problem), download it and use as font.



    Thanks.










    share|improve this question

























      3












      3








      3


      1






      Is it possible to download .ttf file from web and store it on iPhone. Then use that for for labels and all other stuff ? Because my client want to control fonts from database and don't want to just drop fonts to xcode project right away.



      So in future if he wants to change font, he will add new font to database, app will recognize new font on web (thats already done with images, not a problem), download it and use as font.



      Thanks.










      share|improve this question














      Is it possible to download .ttf file from web and store it on iPhone. Then use that for for labels and all other stuff ? Because my client want to control fonts from database and don't want to just drop fonts to xcode project right away.



      So in future if he wants to change font, he will add new font to database, app will recognize new font on web (thats already done with images, not a problem), download it and use as font.



      Thanks.







      objective-c database download nsfilemanager uifont






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 21 '13 at 10:04









      user1832330user1832330

      125219




      125219
























          5 Answers
          5






          active

          oldest

          votes


















          1














          The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.



          You'll have to think in other way of implementing it.






          share|improve this answer
























          • Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

            – user1832330
            May 21 '13 at 10:15











          • It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

            – Antonio MG
            May 21 '13 at 10:16











          • Thank you on your help.

            – user1832330
            May 21 '13 at 10:20





















          3














          Actually it is possible to dynamically add fonts to the iOS runtime like this:



          NSData *fontData = /* your font-file data */;
          CFErrorRef error;
          CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
          CGFontRef font = CGFontCreateWithDataProvider(provider);
          if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
          CFStringRef errorDescription = CFErrorCopyDescription(error)
          NSLog(@"Failed to load font: %@", errorDescription);
          CFRelease(errorDescription);
          }
          CFRelease(font);
          CFRelease(provider);


          Source: This Blog Article of Marco Arment.






          share|improve this answer































            1














            It is possible. I created an example swift project in github. You have to just add the few line below.



            var uiFont : UIFont?
            let fontData = data

            let dataProvider = CGDataProviderCreateWithCFData(fontData)
            let cgFont = CGFontCreateWithDataProvider(dataProvider)

            var error: Unmanaged<CFError>?
            if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
            {
            print("Error loading Font!")
            } else {
            let fontName = CGFontCopyPostScriptName(cgFont)
            uiFont = UIFont(name: String(fontName) , size: 30)
            }


            Github project link



            enter image description here






            share|improve this answer































              0














              You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.






              share|improve this answer
























              • I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                – user1832330
                May 21 '13 at 10:17











              • FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                – calimarkus
                May 21 '13 at 10:33



















              0














              Swift 4 solution by extension:



              extension UIFont {

              /**
              A convenient function to create a custom font with downloaded data.

              - Parameter data: The local data from the font file.
              - Parameter size: Desired size of the custom font.

              - Returns: A custom font from the data. `nil` if failure.

              */
              class func font(withData data: Data, size: CGFloat) -> UIFont? {

              // Convert Data to NSData for convenient conversion.
              let nsData = NSData(data: data)

              // Convert to CFData and prepare data provider.
              guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
              let dataProvider = CGDataProvider(data: cfData),
              let cgFont = CGFont(dataProvider) else {
              print("Failed to convert data to CGFont.")
              return nil
              }

              // Register the font and create UIFont.
              var error: Unmanaged<CFError>?
              CTFontManagerRegisterGraphicsFont(cgFont, &error)
              if let fontName = cgFont.postScriptName,
              let customFont = UIFont(name: String(fontName), size: size) {
              return customFont
              } else {
              print("Error loading Font with error: (String(describing: error))")
              return nil
              }

              }
              }


              Usage:



              let customFont = UIFont.font(withData: data, size: 15.0)





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16667327%2fdownload-font-ttf-file-from-web-and-store-on-iphone%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.



                You'll have to think in other way of implementing it.






                share|improve this answer
























                • Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                  – user1832330
                  May 21 '13 at 10:15











                • It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                  – Antonio MG
                  May 21 '13 at 10:16











                • Thank you on your help.

                  – user1832330
                  May 21 '13 at 10:20


















                1














                The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.



                You'll have to think in other way of implementing it.






                share|improve this answer
























                • Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                  – user1832330
                  May 21 '13 at 10:15











                • It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                  – Antonio MG
                  May 21 '13 at 10:16











                • Thank you on your help.

                  – user1832330
                  May 21 '13 at 10:20
















                1












                1








                1







                The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.



                You'll have to think in other way of implementing it.






                share|improve this answer













                The fonts have to be set in the plist of your app, and that file cannot be changed during runtime, so you need to compile your project with the fonts already added to it.



                You'll have to think in other way of implementing it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 21 '13 at 10:09









                Antonio MGAntonio MG

                19.3k33459




                19.3k33459













                • Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                  – user1832330
                  May 21 '13 at 10:15











                • It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                  – Antonio MG
                  May 21 '13 at 10:16











                • Thank you on your help.

                  – user1832330
                  May 21 '13 at 10:20





















                • Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                  – user1832330
                  May 21 '13 at 10:15











                • It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                  – Antonio MG
                  May 21 '13 at 10:16











                • Thank you on your help.

                  – user1832330
                  May 21 '13 at 10:20



















                Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                – user1832330
                May 21 '13 at 10:15





                Thats what I also thought... Well I will tell him to add all desired fonts to xcode projects and then just to adjust which to be used from database... So I can't open font from another self created plist ?

                – user1832330
                May 21 '13 at 10:15













                It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                – Antonio MG
                May 21 '13 at 10:16





                It has to be in the main plist of the app, and that's created during compile time, so there's nothing you can dom, sorry. Please accept the answer if it was useful.

                – Antonio MG
                May 21 '13 at 10:16













                Thank you on your help.

                – user1832330
                May 21 '13 at 10:20







                Thank you on your help.

                – user1832330
                May 21 '13 at 10:20















                3














                Actually it is possible to dynamically add fonts to the iOS runtime like this:



                NSData *fontData = /* your font-file data */;
                CFErrorRef error;
                CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
                CGFontRef font = CGFontCreateWithDataProvider(provider);
                if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
                CFStringRef errorDescription = CFErrorCopyDescription(error)
                NSLog(@"Failed to load font: %@", errorDescription);
                CFRelease(errorDescription);
                }
                CFRelease(font);
                CFRelease(provider);


                Source: This Blog Article of Marco Arment.






                share|improve this answer




























                  3














                  Actually it is possible to dynamically add fonts to the iOS runtime like this:



                  NSData *fontData = /* your font-file data */;
                  CFErrorRef error;
                  CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
                  CGFontRef font = CGFontCreateWithDataProvider(provider);
                  if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
                  CFStringRef errorDescription = CFErrorCopyDescription(error)
                  NSLog(@"Failed to load font: %@", errorDescription);
                  CFRelease(errorDescription);
                  }
                  CFRelease(font);
                  CFRelease(provider);


                  Source: This Blog Article of Marco Arment.






                  share|improve this answer


























                    3












                    3








                    3







                    Actually it is possible to dynamically add fonts to the iOS runtime like this:



                    NSData *fontData = /* your font-file data */;
                    CFErrorRef error;
                    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
                    CGFontRef font = CGFontCreateWithDataProvider(provider);
                    if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
                    CFStringRef errorDescription = CFErrorCopyDescription(error)
                    NSLog(@"Failed to load font: %@", errorDescription);
                    CFRelease(errorDescription);
                    }
                    CFRelease(font);
                    CFRelease(provider);


                    Source: This Blog Article of Marco Arment.






                    share|improve this answer













                    Actually it is possible to dynamically add fonts to the iOS runtime like this:



                    NSData *fontData = /* your font-file data */;
                    CFErrorRef error;
                    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
                    CGFontRef font = CGFontCreateWithDataProvider(provider);
                    if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
                    CFStringRef errorDescription = CFErrorCopyDescription(error)
                    NSLog(@"Failed to load font: %@", errorDescription);
                    CFRelease(errorDescription);
                    }
                    CFRelease(font);
                    CFRelease(provider);


                    Source: This Blog Article of Marco Arment.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 28 '13 at 12:35









                    calimarkuscalimarkus

                    9,23712145




                    9,23712145























                        1














                        It is possible. I created an example swift project in github. You have to just add the few line below.



                        var uiFont : UIFont?
                        let fontData = data

                        let dataProvider = CGDataProviderCreateWithCFData(fontData)
                        let cgFont = CGFontCreateWithDataProvider(dataProvider)

                        var error: Unmanaged<CFError>?
                        if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
                        {
                        print("Error loading Font!")
                        } else {
                        let fontName = CGFontCopyPostScriptName(cgFont)
                        uiFont = UIFont(name: String(fontName) , size: 30)
                        }


                        Github project link



                        enter image description here






                        share|improve this answer




























                          1














                          It is possible. I created an example swift project in github. You have to just add the few line below.



                          var uiFont : UIFont?
                          let fontData = data

                          let dataProvider = CGDataProviderCreateWithCFData(fontData)
                          let cgFont = CGFontCreateWithDataProvider(dataProvider)

                          var error: Unmanaged<CFError>?
                          if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
                          {
                          print("Error loading Font!")
                          } else {
                          let fontName = CGFontCopyPostScriptName(cgFont)
                          uiFont = UIFont(name: String(fontName) , size: 30)
                          }


                          Github project link



                          enter image description here






                          share|improve this answer


























                            1












                            1








                            1







                            It is possible. I created an example swift project in github. You have to just add the few line below.



                            var uiFont : UIFont?
                            let fontData = data

                            let dataProvider = CGDataProviderCreateWithCFData(fontData)
                            let cgFont = CGFontCreateWithDataProvider(dataProvider)

                            var error: Unmanaged<CFError>?
                            if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
                            {
                            print("Error loading Font!")
                            } else {
                            let fontName = CGFontCopyPostScriptName(cgFont)
                            uiFont = UIFont(name: String(fontName) , size: 30)
                            }


                            Github project link



                            enter image description here






                            share|improve this answer













                            It is possible. I created an example swift project in github. You have to just add the few line below.



                            var uiFont : UIFont?
                            let fontData = data

                            let dataProvider = CGDataProviderCreateWithCFData(fontData)
                            let cgFont = CGFontCreateWithDataProvider(dataProvider)

                            var error: Unmanaged<CFError>?
                            if !CTFontManagerRegisterGraphicsFont(cgFont, &error)
                            {
                            print("Error loading Font!")
                            } else {
                            let fontName = CGFontCopyPostScriptName(cgFont)
                            uiFont = UIFont(name: String(fontName) , size: 30)
                            }


                            Github project link



                            enter image description here







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 2 '15 at 13:47









                            Gyuri TGyuri T

                            155112




                            155112























                                0














                                You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.






                                share|improve this answer
























                                • I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                  – user1832330
                                  May 21 '13 at 10:17











                                • FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                  – calimarkus
                                  May 21 '13 at 10:33
















                                0














                                You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.






                                share|improve this answer
























                                • I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                  – user1832330
                                  May 21 '13 at 10:17











                                • FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                  – calimarkus
                                  May 21 '13 at 10:33














                                0












                                0








                                0







                                You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.






                                share|improve this answer













                                You could use FontLabel (https://github.com/vtns/FontLabel) or smth. similar to load ttfs from the file system. I don't think that you can use downloaded fonts with a UILabel. Because you need the plist entries for each font.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 21 '13 at 10:09









                                calimarkuscalimarkus

                                9,23712145




                                9,23712145













                                • I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                  – user1832330
                                  May 21 '13 at 10:17











                                • FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                  – calimarkus
                                  May 21 '13 at 10:33



















                                • I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                  – user1832330
                                  May 21 '13 at 10:17











                                • FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                  – calimarkus
                                  May 21 '13 at 10:33

















                                I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                – user1832330
                                May 21 '13 at 10:17





                                I would download ttf from database and then when downloaded and added to plist (system or new, self created) use them for labels or any other text. Possible ?

                                – user1832330
                                May 21 '13 at 10:17













                                FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                – calimarkus
                                May 21 '13 at 10:33





                                FontLabel is a replacement for UILabel. Before iOS 4 it was the only option to use custom fonts at all. With FontLabel you don't need a plist entry - so your download approach should work.

                                – calimarkus
                                May 21 '13 at 10:33











                                0














                                Swift 4 solution by extension:



                                extension UIFont {

                                /**
                                A convenient function to create a custom font with downloaded data.

                                - Parameter data: The local data from the font file.
                                - Parameter size: Desired size of the custom font.

                                - Returns: A custom font from the data. `nil` if failure.

                                */
                                class func font(withData data: Data, size: CGFloat) -> UIFont? {

                                // Convert Data to NSData for convenient conversion.
                                let nsData = NSData(data: data)

                                // Convert to CFData and prepare data provider.
                                guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
                                let dataProvider = CGDataProvider(data: cfData),
                                let cgFont = CGFont(dataProvider) else {
                                print("Failed to convert data to CGFont.")
                                return nil
                                }

                                // Register the font and create UIFont.
                                var error: Unmanaged<CFError>?
                                CTFontManagerRegisterGraphicsFont(cgFont, &error)
                                if let fontName = cgFont.postScriptName,
                                let customFont = UIFont(name: String(fontName), size: size) {
                                return customFont
                                } else {
                                print("Error loading Font with error: (String(describing: error))")
                                return nil
                                }

                                }
                                }


                                Usage:



                                let customFont = UIFont.font(withData: data, size: 15.0)





                                share|improve this answer




























                                  0














                                  Swift 4 solution by extension:



                                  extension UIFont {

                                  /**
                                  A convenient function to create a custom font with downloaded data.

                                  - Parameter data: The local data from the font file.
                                  - Parameter size: Desired size of the custom font.

                                  - Returns: A custom font from the data. `nil` if failure.

                                  */
                                  class func font(withData data: Data, size: CGFloat) -> UIFont? {

                                  // Convert Data to NSData for convenient conversion.
                                  let nsData = NSData(data: data)

                                  // Convert to CFData and prepare data provider.
                                  guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
                                  let dataProvider = CGDataProvider(data: cfData),
                                  let cgFont = CGFont(dataProvider) else {
                                  print("Failed to convert data to CGFont.")
                                  return nil
                                  }

                                  // Register the font and create UIFont.
                                  var error: Unmanaged<CFError>?
                                  CTFontManagerRegisterGraphicsFont(cgFont, &error)
                                  if let fontName = cgFont.postScriptName,
                                  let customFont = UIFont(name: String(fontName), size: size) {
                                  return customFont
                                  } else {
                                  print("Error loading Font with error: (String(describing: error))")
                                  return nil
                                  }

                                  }
                                  }


                                  Usage:



                                  let customFont = UIFont.font(withData: data, size: 15.0)





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Swift 4 solution by extension:



                                    extension UIFont {

                                    /**
                                    A convenient function to create a custom font with downloaded data.

                                    - Parameter data: The local data from the font file.
                                    - Parameter size: Desired size of the custom font.

                                    - Returns: A custom font from the data. `nil` if failure.

                                    */
                                    class func font(withData data: Data, size: CGFloat) -> UIFont? {

                                    // Convert Data to NSData for convenient conversion.
                                    let nsData = NSData(data: data)

                                    // Convert to CFData and prepare data provider.
                                    guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
                                    let dataProvider = CGDataProvider(data: cfData),
                                    let cgFont = CGFont(dataProvider) else {
                                    print("Failed to convert data to CGFont.")
                                    return nil
                                    }

                                    // Register the font and create UIFont.
                                    var error: Unmanaged<CFError>?
                                    CTFontManagerRegisterGraphicsFont(cgFont, &error)
                                    if let fontName = cgFont.postScriptName,
                                    let customFont = UIFont(name: String(fontName), size: size) {
                                    return customFont
                                    } else {
                                    print("Error loading Font with error: (String(describing: error))")
                                    return nil
                                    }

                                    }
                                    }


                                    Usage:



                                    let customFont = UIFont.font(withData: data, size: 15.0)





                                    share|improve this answer













                                    Swift 4 solution by extension:



                                    extension UIFont {

                                    /**
                                    A convenient function to create a custom font with downloaded data.

                                    - Parameter data: The local data from the font file.
                                    - Parameter size: Desired size of the custom font.

                                    - Returns: A custom font from the data. `nil` if failure.

                                    */
                                    class func font(withData data: Data, size: CGFloat) -> UIFont? {

                                    // Convert Data to NSData for convenient conversion.
                                    let nsData = NSData(data: data)

                                    // Convert to CFData and prepare data provider.
                                    guard let cfData = CFDataCreate(kCFAllocatorDefault, nsData.bytes.assumingMemoryBound(to: UInt8.self), nsData.length),
                                    let dataProvider = CGDataProvider(data: cfData),
                                    let cgFont = CGFont(dataProvider) else {
                                    print("Failed to convert data to CGFont.")
                                    return nil
                                    }

                                    // Register the font and create UIFont.
                                    var error: Unmanaged<CFError>?
                                    CTFontManagerRegisterGraphicsFont(cgFont, &error)
                                    if let fontName = cgFont.postScriptName,
                                    let customFont = UIFont(name: String(fontName), size: size) {
                                    return customFont
                                    } else {
                                    print("Error loading Font with error: (String(describing: error))")
                                    return nil
                                    }

                                    }
                                    }


                                    Usage:



                                    let customFont = UIFont.font(withData: data, size: 15.0)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 24 '18 at 3:20









                                    Tim ChenTim Chen

                                    904812




                                    904812






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16667327%2fdownload-font-ttf-file-from-web-and-store-on-iphone%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown