For my iOS project i need to use custom fonts. I added fonts in Resources/Fonts folder. And register the fonts in Info.plist like this. In storyboard i am getting custom fonts like this but the fonts not affecting in design. The font style drop down always empty see here "www.tiikoni.com/tis/view/?id=1f07d04". I m working in .storyboard file not .xib
What have i done wrong?
Xamarin studio version - 5.10.3
Fonts used - Exo-SemiBold.ttf, Montserrat-Bold.ttf
Ran into a similar problem. My custom fonts didn´t show up in Xcode at all.
Solution was to move fonts from /Resources/Fonts/ up to /Resources/ (And updating Info.plist accordingly). Doing so made them show up in Xcode.
Try setting font programmatically.
After adding reference of your Font file in plist using "Fonts provided by application" key.
Then Log available fonts in AppDelegate method like this
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
In these logs you can find exact name of the font you want to apply.
Related
I am designing an iOS (SpriteKit) app and have found the Skia (substitute this for any unknown font) font in the list of available fonts in the font-picker if I add an SKLabelNode to the scene. However, when I run the app in the simulator a substitute font appears.
As far as I know, this font came with Xcode. I certainly didn't buy it, although it could have come as part of another application.
Using a font like Chalkduster seems to work OK and does not require an additional work.
So I am wondering if this font requires any special actions to get it to be recognised (e.g., is it like a custom font) or is there some other issue.
Furthermore, if this isn't a standard Xcode font, could I get to get in to trouble for using it in a commercial app?
EDIT: it would be appreciated if answers could state (a) whether this font is visible in their version of Xcode and (b) whether it works.
EDIT2: OK, so a bit more research. I downloaded and installed a real (free) custom font and this now appears in the font-picker, but it is not actually rendered by my app.
So it appears that Xcode shows all fonts installed on the system, whether they are accessible by your app/target device or not. However, you can only use it by following the procedures described in the answers below. This is probably a good thing as it would stop you using accidentally using a font that you have acquired and for which perhaps you don't have the licence. But is a bit annoying that fonts you can't use are offered in the font-picker.
Skia is not part of the fonts included with iOS. You can get a list of all preinstalled iOS fonts from iosfonts.com or wptechonlogy.com
You can list all installed fonts on your device by doing this:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++) {
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
(source for above code)
To install a font, you can either use an app like iOS Font Maker or use code to do it yourself. The steps involved are:
Include your fonts in your Xcode project.
Make sure that they’re included in the target.
Check that your fonts are included as Resources in your bundle.
Include your iOS custom fonts in your application plist.
Use UIFont and specify the name of the font.
Codewithchris.com has a step by step tutorial on how to add a custom font.
Regarding your last question, yes. If the font is not provided as part of the iOS fonts you will have to obtain its license (some are free but some are not so make sure to be on the safe side).
Check this license page for Skia
So I am still wondering why I can see it in the Xcode font picker
Xcode is running on your computer. Once you choose Custom as the Font pop-up selection for an interface object in Interface Builder (such as a UILabel or, as you have discovered, an SKLabelNode), the Family pop-up shows all the fonts on your computer.
I'm trying to use fonts from the Open Sans family. I added the fonts to my Xcode project, I checked that they were added to my application's resources bundle and I added the fonts to my Info.plist file.
When I edit a XIB in Interface Builder, I can use Open Sans font on UILabels when selecting Custom in the font dropdown menu. The font are correctly rendered on the preview in Interface Builder, but then when I launch the application on the device, the font is not applied to the labels.
I tried setting the font programmatically, but that didn't work either. And I'm not getting any warning nor error.
What did I forgot to be able to use a custom font?
The behavior has been the same on an iPad Air running iOS7 and on an iPhone 6 running iOS8.
Go to Target -> Build Phases -> Copy Bundle Resources , in there you check if the font file is there, if not press the plus button and add it manually. Sometimes the font files are not added automatically to the bundle resources.
Hope this helps.
Just make sure all the fonts really are available in the bundle, try printing all fonts and check if you are using the correct name.
You can very easily do this with following code in app delegate's didFinishLaunchingWithOptions method:
for (NSString *familyName in [UIFont familyNames]) {
NSLog(#"Family Name : %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"\tFont Name : %#", fontName);
}
}
or in Swift:
if let familyNames = UIFont.familyNames() as? [String] {
for familyName in familyNames {
println("Family : " + familyName)
if let fontNames = UIFont.fontNamesForFamilyName(familyName) as? [String] {
for fontName in fontNames {
println("\tFont : " + fontName)
}
}
}
}
The swift code is not the most efficient, but should work for checking if the fonts exist in the bundle.
What worked for me was when I added the font to check the "Add to targets" thick in front of my app name.
See image example here :
Ensure you have added the fonts in your Info.plist. There is an entry called "Fonts provided by application" which must contain all font files you want to use.
I just had the same problem... still kind of having... I wanted to mix fonts in the same label, which did not work...
The workaround was that I tried to change in the IB attributes inspector of the label try to change Text dropdown from Attributed to Plain... The font did change in the device... so I think this is a bug in Xcode.
Are you adding the font to your info.plist exactly how the font is spelt with the inclusion of its file extension? For example, OpenSans.ttf is different than opensans.ttf or just opensans. To be 100% sure of the file name right click on your font file, click on Get Info, and then check the Full Name description.
To add the font programmatically leave the file extension off. For example,
myLabel.font = [UIFont fontWithName:#"OpenSans" size:32];
If you are using webfont then download.ttf file and drop it into your project . Check mark on copy items if needed
Next add this on info plist
<key>UIAppFonts</key>
<array>
<string>Your fontname.ttf</string>
<string>Bakersfield Bold.ttf</string>
</array>
Now take a look the font family name. Which you will find on font file also. From where you have downloaded you will get there also. Like i added font which ttf file name is : Bakersfield Bold.ttf for this fontname is : Bakersfield-Bold
Thats it Happy coding.
I'm developing a game and I would like to use a custom font in my app. I'm using SpriteKit for it, if that's important. I've tried using this https://github.com/deni2s/IBCustomFonts but I cannot get it to work with this font http://www.fontspace.com/freaky-fonts/emulogic
I've tried various things, and I've registered the font on the info.plist of the app... Does anyone know what's going on?
First of all I'm assuming that SpriteKit doesn't make any difference.
You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
You need to edit .plist file. Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.
Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:
UIFont *customFont = [UIFont fontWithName:#"JosefinSansStd-Light" size:20];
In iOS8 you add your fonts directly to the project and they are visible in the interface builder.
Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if (SYSTEM_VERSION_LESS_THAN(#"8.0")) {
UIFont *customFont = [UIFont fontWithName:#"OpenSans-Light" size:32];
self.registerLabel.font = customFont;
}
No problem to use custom fonts, I did it. Follow these steps:
Register the font in your info.plist adding the "Fonts provided by application" entry. Put the font file name, not the font name. Like font.ttf (I think you already did that)
Go to your project on the project navigator, click on the project name and go to "build phases" then look down in "Copy Bundle Resources" go to the bottom, there are a lot of files and search for the "+" sign. A popup window will open with your project files. Search for your font file and add it to the bundle.
Finally check the font name. When you use it in your code, you have to use the font name, not the file name. To know the font name, double click on the font file in Finder and a window will open showing the font. Check that window title, that's the name of the font that you have to use.
And advice, add that name as a macro in your ..Prefix.pch file. It will be easier to use it in your project if you have to use the font multiple times.
The problem that I think you had is that you properly registered the font but it was not copied to the app bundle. So when the app executed it couldn't find the font. That's why step 2 is important. I spent a lot of time trying to figure that out the first time :)
To achieve this in swift
If you would like to list all your fonts when the app starts just to see useable name for your font. You can do that with this little piece of code:
UIFont.familyNames.forEach {
print("fontFamily: \($0), fonts: \(UIFont.fontNames(forFamilyName: $0))")
}
I have never been able to get these answers to work having tried multiple times. The only way I have ever got a custom font in it to follow the instruction in the accepted answer then create a hidden label in the story board that uses the font. The I can use the font programatically. Weird and hacky I know but it worked for me.
If you're in Xcode 6 it's as easy as it gets. Just add a font folder into your project, add your font(s) into the folder, and you're good to go.
If none of the methods above worked for you, AND you happen to use your custom font "programatically" only (i.e. you didn't use it in any xib) then you will need to use it at least once somewhere in one xib, anywhere.
(P.S. Stuart Clark - already answered this, so no credit for me please. However, I decided to rephrase and post it as a separate answer, because I missed his answer, it wasn't clear to me when I read it. I found out this solution by chance when I was testing the custom font in a xib, and I saw that it worked elsewhere programatically, but when I removed the font from the xib, it stopped working programatically.)
Updated for Swift 3.
let fontFamilies:[String] = UIFont.familyNames
for fontFamily: String in fontFamilies {
let fontNames: [String] = UIFont.fontNames(forFamilyName: fontFamily)
print("\(fontFamily) \(fontNames)");
}
Maybe a quick fix - this did it for me when the font has already worked, but a new e.g. Label didn't show the desired font:
clean, deep clean and empty derived data folder.
please try below code.
#end
#implementation UILabel (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
#end
#implementation UITextField (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
#end
#implementation UIButton (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.titleLabel.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self.titleLabel setFont:[UIFont fontWithName:self.titleLabel.font.fontName size:newsize]];
}
#end
#end
#interface UILabel (CustomFont)
-(void)setFontName:(NSString *)value;
#end
#interface UITextField (CustomFont)
-(void)setFontName:(NSString *)value;
#end
#interface UIButton (CustomFont)
-(void)setFontName:(NSString *)value;
#end
I've never had this problem before. But I can't get any font to show up besides the system font. I wanted to use helvetica, but that wasn't working, I have tried setting my UILabel's to Farah just to see if it would work and it doesn't. However, if I run in the simulator (with iOS7) then it seems to work fine. I've tried setting fonts on the storyboard. Then tried setting it programatically,
self.titleLabel.font = [UIFont fontWithName:#"Farah" size:25];
Has anything changed with the new Xcode that won't allow non-system fonts?
Add all non-system fonts to your application and include to .plist file in 'Fonts provided by application' section.
After you can use fonts in your application : [UIFont fontWithName: #"TitilliumText25L-400wt" size:14];(replacing "TitilliumText25L-400wt" to corresponding font name).
P.S. If you have a problems with detection of real font name use FontBook application or
the code snippet bellow which show you all available fonts for your application.
for(NSString* family in [UIFont familyNames]) {
NSLog(#"%#", family);
for(NSString* name in [UIFont fontNamesForFamilyName: family]) {
NSLog(#" %#", name);
}
}
The parameter of fontWithName must be the real name of the font, not the name of the file.
If you open your ttf file with the Mac Font Book, you will directly see its name on top of the window.
For more, check this out.
Getting font names right is not easy. Run this code to learn the available names:
for (NSString* s in [UIFont familyNames])
NSLog(#"%#: %#", s, [UIFont fontNamesForFamilyName:s]);
Of course, if setting a feature of self.titleLabel does not work, you should also make sure that self.titleLabel is not nil.
I have a problem with custom font. Namely it's Tw Cen MT.ttf
It's the same problem as in here:
Custom font not showing on device, but does on simulator
Except the given solution doesn't work for me.
I have done exactly what you should do:
Added Font to Info.plist
Added Font to Copy Bundle Resources section in Build Phases.
Used font programmatically by name not filename.
It still doesn't work. What's more interesting - when I list available fonts:
NSLog(#"%#", [UIFont familyNames]);
It shows me my font on simulator but does not show it on the device. I tested it both on the new iPad and iPhone 4 with iOS 6.
Any advice?
Be sure not to confuse the family name with the font name. This is the code I use to check the real name of my fonts.-
NSArray *familyNames = [UIFont familyNames];
for (NSString *aFamilyName in familyNames) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:aFamilyName];
for (NSString *aFontName in fontNames) {
NSLog(#"%#", aFontName);
}
}
Make sure you got the name of the font right. The simulator is not case sensitive whereas the phone is.