I need OpenSans in my app and so I imported the whole OpenSans bunch which includes Bold, ExtraBold, Italic, Regular, Light and more. I added them to Fontbook (to check the exact name I need to address them by when using them) on my Mac, to my project in the file structure and added them in Build phases to my project as well.
Now the weird thing; when I use them with UIFont, ONLY OpenSans-Bold works. The rest doesn't work. If I use OpenSans-Bold (exactly the identifier given by Fontbook), everything works fine. If I, however, change it to OpenSans-Regular or OpenSans-Light or something else in that family, I get the systemfont with UIFont and I get nothing when drawing with Quartz.
All fonts are in all lists and I checked all the identifiers from Fontbook (which was why I couldn't get OpenSans-Bold working), but now i'm at a loss. Any idea what I could be doing wrong?
OpenSans Regular is just OpenSans, semi-bold is OpenSans-Semibold, etc.
In Font Book.app select the font and choose Command + I which brings up the font info. In the "PostScript name" in the top you can see what the font is called and how you can call it in your code.
Also, just make sure the font has actually been added in your bundle and your plist :)
You Can check the fonts provided by the ttf file With the following lines of code..
this method is provided by the UIFont class.
(NSArray *)fontNamesForFamilyName:(NSString *)familyName
You need to add the files in the right locations, then in the plist file, then enumerate them all and print them to get the PDF names so you can access them. Most likely you're using the wrong names.
Fonts have three kinds of names:
The file name
The font name in Font Book
The PDF name - this is the one you use to access the font in code
See this answer:
Adding custom fonts to iOS app finding their real names
Related
I have read through previous threads on this topic, but all are old, unanswered, and/or about something more specific about creating custom fonts.
In TCPDF:
$pdf->SetFont('times', '', 10);
The pdfs I create look awesome. It is just that BarnesAndNoble says my submitted PDF does not contain "embedded fonts". Kindle says the same, but that they then embedded them for me. I'm trying LuLu next, but want to deal with BN.com first.
An example suggests:
$fontname = $pdf->addTTFfont('/fonts/arial.ttf', '', '', 32);
... but does not go on to say what to do with "$fontname". Nor what the "32" specifically stands for, or if there are better options for that "32". Or, if THAT does embed fonts!
My webhost says my fonts path is:
/usr/share/fonts/default/Type1
... maybe then (case sensitive??):
/usr/share/fonts/default/Type1/Helvetica
I need to properly embed my fonts!
TCPDF will just automatically embed fonts you've added on its own. I'm fairly certain it's presumed that most all OS's have Times, Helvetica and Courier already available and therefore TCPDF does not embed them. So in your case, if you add a TTF font TCPDF should recognize when it is used and embed the font definition automatically.
Also, you should only have to do $fontname = $pdf->addTTFfont('/fonts/arial.ttf', '', '', 32); once, so you could run your script once, and then comment that line out.
Direct From the documentation;
AddFont( $family, $style = '', $fontfile = '', $subset = 'default' )
Imports a TrueType, Type1, core, or CID0 font and makes it available. It is necessary to generate a font definition file first (read /fonts/utils/README.TXT). The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
Parameters
$family -
Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
$style - Font style. Possible values are (case insensitive):
empty string: regular (default)
B: bold
I: italic
BI or IB: bold italic
$fontfile - The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
$subset - if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
Returns - array containing the font data, or false in case of error.
Since
1.5
See
TCPDF::SetFont(), TCPDF::setFontSubsetting()
Public
I tried to use custom font in Xamarin iOS using this guide. I added the fonts to Resources/Fonts folder. And then added it to the info.plist under the array property Fonts provided by application. I changed the property of font to Bundle Resource and Copy Always.
Now, the newly added font have to be shown in xamarin designer. But, it is not showing for Roboto-Regular.ttf. But It is working fine for OpenSans-Regular.ttf.
Any help are welcome!
iOS may register the font under different font name. If your font was successfully registered (as your steps indicate you did), you can find it inside UIFonts.FontFamilyNames array and then use that. To find all registered fonts,
foreach (var fontFamily in UIFont.FamilyNames)
{
foreach (var font in UIFont.FontNamesForFamilyName(font))
{
System.Diagnostics.Debug.Console.WriteLine(item);
}
System.Diagnostics.Debug.Console.WriteLine("---");
}
if you still don't see your font. Try to find it explicitly by name,
foreach (var font in UIFont.FontNamesForFamilyName(friendlyFontName)) // can have space in font name.
{
System.Diagnostics.Debug.Console.WriteLine(item);
}
System.Diagnostics.Debug.Console.WriteLine("---");
If you find your font there, that's what you need to use as font name in your code.
Example,
I recently used Bbas Neue. The file names where,
BebasNeue Regular.ttf
BebasNeue Bold.ttf
BebasNeue Thin.ttf
In android, I used BebasNeue Regular.ttf#BebasNeue Regular and it worked as expected. In iOS, however, the font names were as follows,
BebasNeueRegular
BebasNeueBold
BebasNeue-Thin
as you can see, not in any pattern. So after looking for font family Bebas Neue (with the space), I found the correct names, and was able to use the font on iOS.
Hope this helps.
I have added a custom font to my project.
It is included in the target, and it is added in the plist.
When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts.
However, it does show up as an option in Interface Builder, and if I set a label's text to that font in IB, it works correctly and shows up when I print out a list of available fonts.
This is XCode 6.4 and iOS 8.0
When it is working via IB, it gets printed out in the font names like this:
Special Elite
SpecialElite-Regular
I call the font programmatically like:
[UIFont fontWithName:#"SpecialElite-Regular" size:fontSize];
There are no problems when doing this with the built-in fonts.
When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts
This proves that you have not in fact included it properly in the target and the Info.plist.
The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.
Your font is Special Elite. As you can see, I have it visible here in my running app:
Here's the code that I used:
let lab = UILabel()
lab.text = "This is a test"
lab.font = UIFont(name:"SpecialElite-Regular", size:18)
lab.sizeToFit()
lab.frame.origin = CGPointMake(100,100)
self.view.addSubview(lab)
So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.
(Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)
Ok, topic is old but I would like to put in my two cents here.
First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
In brief:
remember to include your fonts to project
setup target for fonts
check if your fonts are included as resources in bundle
add custom fonts to plist file
check the real name of your font in code
When I did everything like described in this article, my fonts works in interface builder only. I also have problem with step 5 - my fonts did not show up in console output.
Ok, so now is time for tips from me:
TIP #1
use otf font format, if you have different, convert it (or ask your designer ;) )
you can use this site for that: https://onlinefontconverter.com/
TIP #2
In article above you can find:
Open it and add a new row called “Fonts provided by application” which
will be an array that you need to add all the filenames of the fonts
you want to use.
Instead of "Fonts provided by application" enter "UIAppFonts"
Remember, if you have more than one plist file, set this for all.
As mentioned by others, following article is very useful to diagnose the problem.
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored. If you add that directory, fonts will be visible in Interface Builder but not in the running app.
Matt did a great job of isolating the problem... I was not loading the font correctly.
In my case the problem was in the Info.plist.
I was listing the font in the Info.plist using its name. This is incorrect. You are supposed to list the font in the Info.plist using its file name.
I know this is old but I just ran into the same issue. The font would show up only if I selected that font in the interface builder. Turns out when I searched for UIAppFonts, it didn't list the main Info.plist (we were already using custom fonts) for some reason. So I ended up adding the fonts to the localized (?) plist file which resulted in this issue. What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.
Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.
for family: String in UIFont.familyNames
{
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
it will prints all fonts, search your font and give proper name.
let attributedText = NSMutableAttributedString(string: "We
Apologize! \n This feature is only available for \n Go 4.0
Subscriptions", attributes: [NSAttributedStringKey.font :
UIFont(name: "SpecialElite-Regular", size: 22)!])
I read that iOS supports .ttc files (font collection) but i don't know how to specify the different font weights with only one file.
I am used to doing it by file name like this
[UIFont fontWithName:#"ProximaNova-Bold" size:17.0f];
but with a .ttc file i have only one file.
Adam's guidance is correct but incomplete if your question is about adding a font not provided by the system.
Add the font to app bundle and make sure that it is part of the 'Copy Bundle Resources' phase. You can also select the font in the project navigator and then check for large membership in the file inspector on the right.
Open your app's plist and add the font's file name in an array for the key UIAppFonts
Get the font's postscript name (as Adam instructed)
Install the font on an OS X machine
Open the font in Font Book
Open the font family and then the font
View > Show Font Info. Note the "PostScript name".
Reference the font in your code using the PostScript name [UIFont fontWithName:#"AppleSymbols" size:24]
You say that you are "used to doing it by file name", however, that isn't how fontWithName:size: works. The "name" field in this case should be the name of the font itself. If it worked in the past with the name of the file, that's only because you got lucky and the name of the file and the font happened to be the same.
There are two ways to find the name of the font:
1) Install the font on an OS X machine, and then, in Font Book, open the font, click on the family you want, and use Preview > Show Font Info. You'll want the "PostScript name".
2) With the app running, stop in the debugger and call [UIFont familyNames]. Find the likely candidate for your font among those names. Then call [UIFont fontNamesForFamilyName:#"Foo"] with the name of your family. Choose the font name that likely corresponds to your font.
Note that this solves the problem of "how to specify different font weights". The answer is that you actually load individual fonts out of the TTC by referencing them by name.
I'm trying to embed the Open Sans font family (ttf files) into Obj c. I've added them to my info.plist and I'm able to use some of the fonts, but (for instance) the OpenSans-CondLight.ttf font doesn't show up when I check all the fonts embedded. Anyone got a possible explanation for this? There is nothing wrong with the ttf files.
Yes. Likely the name of the font file is different than the font name itself. I would look for the font name. You can get this by going to the TFF -> Right Clicking -> Get Info -> "Full Name" (See: How do I get the font name from an otf or ttf file?).