thii / FontAwesome.swift

Use FontAwesome in your Swift projects
MIT License
1.57k stars 264 forks source link

Documentation on how to use FontAwesome pro Icon #232

Open imrabti opened 5 years ago

imrabti commented 5 years ago

Would it be possible to offer an example of how to use this library with FontAwesome pro icons

pgrundner commented 4 years ago

So far, here are some steps that made me get a little further

1.) add the pro *.otf files to your project.

2.) add the following list of font files to your info.plist file as Key UIAppFonts:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Font Awesome 5 Brands-Regular-400.otf</string>
    <string>Font Awesome 5 Duotone-Solid-900.otf</string>
    <string>Font Awesome 5 Pro-Light-300.otf</string>
    <string>Font Awesome 5 Pro-Regular-400.otf</string>
    <string>Font Awesome 5 Pro-Solid-900.otf</string>
</array>
</plist>

3.) in your code, before using FontAwesome.swift the first time, call: FontAwesomeConfig.usesProFonts = true

Thats it. Try to get the icon "fa-camera" with regular style, which is located in the pro version. Looks fine on my side.

I did not succeed to get all icons to work. E.g. fa-abacus is not showing up. Am I missing something in here?

vjoomens commented 4 years ago

Hi all, got pro fonts working, except... lots of icons are missing in the Enum (eg repeat). Seems this file is auto generated. Trying to regenerate didn't work: error. Please help. Is there a way to regenerate this file on xcode build?

vjoomens commented 4 years ago

I figured it out: remove the path: String line in the codegen.swift file (line 20) and it works. Simply use the newly generated FontAwesome enum in your project.

mralexhay commented 4 years ago

@vjoomens Hi! I'm stuck on the same problem you had. How are you running the codegen file to regenerate the enum? The file isn't there when installing as a pod. Thanks!

vjoomens commented 4 years ago

@mralexhay check my fork of this project (https://github.com/vjoomens/FontAwesome.swift). It's fully compatible with FA pro.

mralexhay commented 4 years ago

@vjoomens amazing, this worked for me, thanks.

The only problem I haven't been able to sort is that using the Duotone style throws an error (despite the OTF being stored next to the others and being listed in the Info plist)

CleanShot 2020-03-15 at 12 35 57@2x

but the rest work perfectly.

ghost commented 4 years ago

To implement FA Pro support, follow the instructions from @pgrundner.

To get all FA Pro icons, you need to hack a bit by following this: https://github.com/thii/FontAwesome.swift/issues/230#issuecomment-605018622.

austintt commented 4 years ago

Alright, I've followed the steps listed here and here, but I guess I've done something wrong.

UIFont(name: style.fontName(), size: fontSize)! on line 98 of FontAwesome unexpectedly finds nil for file.

Maybe you guys can help me catch my mistake.

I've added the Pro otf files to the project. The otf files are added to the app target. I've set FontAwesomeConfig.usesProFonts = true. I generated the new enum.swift following the instructions mentioned in the link above.

I added the following to my info.plist under the Fonts provided by application key:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Font Awesome 5 Brands-Regular-400.otf</string>
    <string>Font Awesome 5 Pro-Light-300.otf</string>
    <string>Font Awesome 5 Pro-Regular-400.otf</string>
    <string>Font Awesome 5 Pro-Solid-900.otf</string>
</array>
</plist>

It seems like the file path for my new files is incorrect. Any ideas?

ghost commented 4 years ago

@austintt It would help troubleshooting if you can catch the value of style.fontName() in the debugger. Then we would know which fontName is causing the problems.

A potential cause for your crash could be that even though duotone rendering is not support, you might have to add all the font file nevertheless:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Font Awesome 5 Brands-Regular-400.otf</string>
    <string>Font Awesome 5 Duotone-Solid-900.otf</string>
    <string>Font Awesome 5 Pro-Light-300.otf</string>
    <string>Font Awesome 5 Pro-Regular-400.otf</string>
    <string>Font Awesome 5 Pro-Solid-900.otf</string>
</array>
</plist>
austintt commented 4 years ago

@ghowen, thanks for the suggestion! I moved some stuff around and got a little more detail with the following error from loadFont: com.apple.CoreText.CTFontManagerErrorDomain error 105 - Could not register the font file(s). I tried referencing the duotone font in the plist, but found the same error.

This happens with the first call in my app to FontAwesome, requesting a light styled icon. Placing a breakpoint in loadFontAwesome(ofStyle), I see that the style.fontName returns FontAwesome5Pro-Light. I've tried it with the other styles, and the result is the same.

Here's the full error:

Could not register the font file(s), CTFontManagerErrorFontURLs=(
    "file:///Users/austin/Library/Developer/CoreSimulator/Devices/70E245F8-D1F1-4DCD-8779-A0C8A2F9ACE2/data/Containers/Bundle/Application/9738EF3B-EC31-4401-A88C-BD15A49F3BB3/TaskMinder.app/Font%20Awesome%205%20Pro-Light-300.otf"
ghost commented 4 years ago

@austintt When you added the font files to the project, did you check the "Copy if needed" option? Also make sure that the all font files have the correct target membership. You can check this in the File Inspector when you click on the font files in your project. My current guess is that in your case the font files do not have a target membership.

mralexhay commented 4 years ago

@ghowen Did you ever find a solution to that unexpected nil error? Mine has started doing it and I haven't been able to find the answer yet. Thanks.

ghost commented 4 years ago

@mralexhay You should probably check with @austintt if https://github.com/thii/FontAwesome.swift/issues/232#issuecomment-609626259 did the trick for him. My bet is still that the fonts were not added to the target of the app.

MatrixSenpai commented 4 years ago

My personal recommendation, and how I prefer to do it, is to use the FontBlaster library instead. Make sure the first line in application(didFinishLaunching: with options:) is FontBlaster.blast()

Also ensure that all of the fonts appear in Xcode, and you have added them to the Copy Bundled Resources build phase, found by going to your target -> Build Phases

austintt commented 4 years ago

@ghowen no dice, I had checked to make sure everything was a part of the correct target and copied to the app, everything was as good. For now, I'm just using the free fonts while I focus on other parts of my project. I'll try adding the Pro fonts again later. Thank you for the help though!