How do I use Font Awesome as a custom font in SwiftUI? - swift

I have followed a number of tutorials in adding a custom font to my SwiftUI package. I have take the following steps:
Add Font Awesome 5 Free Solid-900.otf to my project.
Made sure that the font is copied and added to the target
Added the font file names to the Fonts provided by application array in Info.plist
Tried to get the font name
Tried a large number of permutations and combinations for the font name.
I tried the following:
Text(Text("\u{f071} Danger Will Robinson").font(.custom("Font Awesome 5 Free Regular", size: 20))
which is supposed to give me an alert icon, but all I get is a question mark.
Is there a trick to using Font Awesome in this way?
I know there are a few packages available, but I’m trying to learn more about the process itself, and I can’t see that it should be too hard.

OK, I worked it out.
For MacOS, the Info.plist key is: Application fonts resource path, not as above.
The next trick is to get the font name.
One method is to install the font, and then check FontBook. The PostScript name is the name I need.
The other is to run the following code:
let fontManager = NSFontManager.shared
let fonts = fontManager.availableFonts
for name in fonts {
print(name)
}
That’s what happens when all the tutorials are for iOS, not MacOS.

Related

UIFont.fontName in iOS10 doesn't return "regular" suffix

After a successful migration to swift 2.3 in my application, I found a font glitch.
I have an extension for UIFont where I change system font to my custom font.
In iOS 9 and earlier everything worked as expected. In iOS 10 it doesn't.
The problem was in self.fontName. Normally it should return "fontName" + suffix for style (ex:regular, bold, etc).
However in iOS 10 for regular the style, it doesn't return suffix only "fontName".
I googled to found why but couldn't find anything.
The problem is fixed but why this happens I don't know. Any idea?
Using font names has never been a good idea.
Use fontDescriptor and inspect the UIFontDescriptor's fontAttributes and symbolicTraits to obtain information on the font in question. You can then create a new descriptor with modified attributes and/or traits, and obtain a font with that descriptor using init(descriptor:size:).

How to Display different Custom arabic fonts in iPhone application?

In my application i need to display arabic text with different custom fonts. I follow the scenario adding ttf files to info.plist.
As per above scenario i am successfully getting display text in different font style for ENGLISH text only. I am doing same thing for arabic font styles but here i am not getting. Why is going like that?
Please any one can help me
Thanks in Advance.
#Kareem , I took hint from Stackoverflow itself to load the fonts but could not get it working in first go. May be following steps can help you .
Add the font files to your project.
Make their entries in info.plist file
such as
you can now implement some method that loads your font somewhere in
application delegate
like
-(UIFont*) CustomFontWithSize:(float)size{
UIFont* customFont = [UIFont fontWithName:#"FX_Masa" size:size];
if(customFont == nil)
customFont = [UIFont systemFontOfSize:size];
return customFont;
}
If you notice then the name I have passes here is "FX_MASA" and not FX_MasaRegular , this was the point where I was wrong. The name that we need to use while fetching the font is the INSTALL NAME (Double click on the font to install the font on system and the name that appears in Font Book is the install name).
Hope this helps your problem as well. In case you come across any good method, please update here.
There is a nice UILabel extension in GitHub called "Font Label": https://github.com/zynga/FontLabel
This allows to load any TTF file and then draw ZLabel objects (ZLabel is an extension of UILabel) with this custom font. It is based on CoreGraphics and I tested with many custom fonts and proved to work correctly. I don't know of course the effect with arabic fonts, but it's worth a try and a feedback from you (to us and also the github project admins) is welcome.

Custom font in iphone not being displayed properly

I installed a custom font called "modern no. 20" (already installed in my mac) into my iphone project .
I copied the modernno20.ttf into my resources.
Now in my app-info.plist i added this font name in "Fonts provided by application".
Then i added
cell.textLabel.font=[UIFont fontWithName:#"modernno20.ttf" size:14.0];
in my tableviewcode .
i dont know why but this font is not properly displayed.is there any thing i missed?
In order to call the font in your fontWithName call, the string should be the name of the font as it is displayed in the Mac's 'Font Book' app, and not the actual filename of the referenced font file.
There hasn’t been an easy way to add custom fonts to your iPhone applications. As of iOS 4 it has become very easy to do. there could be possibility you would have missed some of the step to get custom font work.
Here is what you need to do in order to add custom fonts,
Check How to include ttf fonts to iOS app

Custom font doesn't work in interface builder!

I've downloaded a font that's called "aldo the apache (.tff) " from dafont.com.
I used it with different programms like adobe illustrator and it seemed to work just fine.
Recently i tried to use it in a game that i'm making to experement. It didn't work -_-.
IB was displaying the font as a slightly bigger version of arial.
How can i solve this problem and get the font to display correctly?
If you have any suggestions, please post them down below.
-DD
You have to edit your <appname>-Info.plist file and create a new UIAppFonts key with type array, where each element is a String with the name of your font file, in this case AldotheApache.ttf. Then use the name in IB or with UIFont as it shows in the application Font Book app of your Mac, in your case Aldo the Apache. Obviously the font should also be added as a resource of your project.

Best way to display barcodes from NSString

Just wondering what is the best way to display a barcode given a string on the iPhone. I have looked over stackoverflow and google and a few people have different ways each requiring a decent amount of work (I think) and also slightly old so I wanted to get it right first time.
One way I've read is using a custom font that you can now use in iOS4, using this font Someone else has written a class to import custom fonts, though this apparently isn't needed for iPads.
I have also found a Library but not much further details on it.
If using the font is the best way is the font linked above good or are there better ones?
Cheers for any help.
Well I went ahead and checked it out anyway. Fonts work extremely well and are very easy to implement. Basically copy this barcode font into your project. Then in your App info.plist type this in:
<key>UIAppFonts</key>
<array>
<string>3OF9_NEW.TTF</string>
</array>
For the above linked font.
Then in where you want to place the barcode just use a UILabel and then for the font use :
label.font = [UIFont fontWithName:#"3 of 9 Barcode" size:40];
Note that the font name is not 30F9_NEW.TTF. If you are using another barcode font or other font in general just open the ttf file in font book and it will say the name up the top of the window. Also you need to have a * at the beginning and end of the string you are using. If you navigated here looking at how to implement a custom font you can do it the same way, just not the font name isn't the file name.