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.
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;
}
Hope this helps, cheers.
No problem to use custom fonts, I did it. Follow these steps:
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 (ie you didn't use it in any xib) then you will need to use it at least once somewhere in one xib, anywhere.
(PS 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 eg 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.