0

Help me please.

There are 2 resource folders and fonts. The second is embedded in the first. How to scan the fonts folder and get a string array of file names.

thanks.

enter image description here

1 Answers1

2

If you have not added the Fonts folder as a folder reference, but as a folder group like in your screenshot, then all of the fonts will actually appear in your main bundle resource directory as individual files, and not in a folder called Fonts. However, if you do add it as a folder reference you can access all of your font paths by doing the following:

 if  let directory = Bundle.main.path(forResource: "Fonts", ofType: ""),
     let files = try? FileManager.default.contentsOfDirectory(atPath: directory) {
     for file in files {
         print(file)
     }
 }

Fonts added as a folder reference

Fonts added as a folder reference

Fonts added as a folder group

Fonts added as a folder group

Alternatively, and not as elegant, if you wish to keep Fonts as a folder group, you could find all of your font files by doing the following instead:

if  let directory = Bundle.main.resourcePath,
    let files = try? FileManager.default.contentsOfDirectory(atPath: directory) {
    for file in (files.filter { ["ttf", "otf"].contains(($0 as NSString).pathExtension)  }) {
        print(file)
    }
}
rolling_codes
  • 15,174
  • 22
  • 76
  • 112