Android RunTime exception "Font asset not found" for .woff font files in android nougat - android-7.0-nougat

I am developing one application, where I need to use .woff fonts. I have written the following code to get font type face from .woff file and set into textview.
hellofont = getFont("fonts/AvenirLTStd-Black.woff");
//welcomefont = getFont ( "fonts/AvenirLTStd-Heavy.woff");
thankufont = getFont("fonts/RobotoCondensed-Bold.ttf");
TextView text1 = (TextView) findViewById(R.id.text1id);
text1.setTypeface(hellofont);
TextView text2 = (TextView) findViewById(R.id.text2id);
text2.setTypeface(thankufont);
public Typeface getFont( String fontName){
try {
Typeface content = Typeface.createFromAsset(this.getAssets(), fontName);
return content;
}
catch(RuntimeException e)
{
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
}
return null;
}
This code works fine on android 5.0 and 6.0 devices. But it is giving exception ( Font asset not found fonts/AvenirLTStd-Black.woff )on android 7.0 devices.
I have tested ttf and otf fonts on android 7.0 devices and it is working fine. Only woff fonts gives this exception.
I have attached the screen shot also which shows the fonts in assests/fonts folder. asset folder with woff fonts
Can anyone help me" what is the issue here?
Thanks in advance
Krishna

WOFF support in Android 7.0 and 7.1 appears to be at least partially broken, with such fonts being unloadable from package assets directories. Your best option, if you are supporting Nougat, is to get TTF or OTF versions of your fonts and use those instead.

Related

react-native add new font gives 'Unrecognized font family' on IOS simulator

I added new font - 'SourceSansPro'(got from GoogleFonts) to my React-Native project. But I got 'Unrecognized font family' error on IOS simulator. There are dozens of various suggestions about this problem like deleting build items or look whether fonts are defined in info.plist or execute 'react-native-link' and etc.However, none of them worked for my case. Also, some people mentioned that, although it does not work through CLI, it works when executed directly through Xcode. Whereas,it did not work for me. I'm stucked at this problem for almost 5 hours. Finally, I thought maybe someone may helps me, here.
Do you have any suggestion?
Your font's name is probably not SourceSansPro
Depending on which one (or more than one) that you added to your project, the name is more likely SourceSansPro-Regular or SourceSansPro-Bold or SourceSansPro-Italic etc
I don't use React-Native, but you can list the Font Families and Font Names available to your app with this code:
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
You need to also make sure that the fonts are included as part of the bundle in the iOS project. First drag the font to the file navigator in Xcode under your project. Then select the font in the file navigator, open the Inspector and make sure it's ticked for your project under Target Membership. Hope this helps.
Please add your custom font to your Plist file like the attached screenshot with key "Fonts provided by application" . And please make sure that the font is successfully loaded by change the font name to custom font from xib , you should see your custom font at the list of fonts at xib .
Thank you
Nada Gamal
The Swift version of the DonMag's answer:
for family: String in UIFont.familyNames{
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family){
print("== \(names)")
}
}

font awesome ios icons all the same

I'm trying to use FontAwesome on my ios app (ios8, xcode6), and am having trouble. I've downloaded the font, tried the TTF and OTF versions separately. I've included the font name in my plist file, and have included the NSString+FontAwesome and UIFont+FontAwesome categories that are standard out there.
The problem is that every font awesome symbol showing is always this same one. Everything works, it just never changes the character.
id github = [NSString fontAwesomeIconStringForEnum:FAGithub];
id twitter = [NSString fontAwesomeIconStringForEnum:FATwitter];
label1.font = [UIFont fontWithName:kFontAwesomeFamilyName size:32.f];
label1.text = [NSString stringWithFormat:#"%# %#", github, twitter];
If I use the unicode string directly instead of the helpers, it is still always the same
#"\uf001"
#"\uf002"
// etc
When I debugged the category classes and printed out the unicode array with all the font-awesome codes in the console, all the symbols were identical to the ones above.
[NSString fontAwesomeUnicodeStrings]
Has anyone seen this problem of the repeating symbol, or know a resolution?
EDIT
2 weeks later, after fixing by re-install: I believe the symbol shown is, which was always the same, is the result of the label's font either not being set correctly to FontAwesome, or the font not being loaded so it is not able to be set. That is the symbol that shows up when font is set to something like "System".
Steps taken for this to work:
File > Add File > Added ios-fontawesome in it entirety (copied folder)
Removed all .m files from fontawesome besides NSString+FontAwesome.m from compile sources
Added FontAwesome.tff to info.plist key Fonts Provided By Application
(Compiled for iPhone 5s Sim, 8.1 using XCode 6.1.1)
Test project listed here: https://github.com/spacedrabbit/StackScratchPad
As the comments indicate, this doesn't explain why it wasn't working it just is the way I was able to make it work.

Does iOS SDK support SVG as custom font?

I have a custom font in SVG-file. I've added it to the application's bundle same as i did it with OTF font. Now I'm trying to use it in code below:
UIFont * customFont = [UIFont fontWithName:#"myFont" size:16.0f];
And I am getting error:
Apr 7 14:58:46 Alexander ExampleProject[26403] : FT_Open_Face failed: error 2.
I'm thinking iOS SDK just not supporting SVG-font yet because the same way works with OTF-font, but does not with SVG. Am i right?
The iOS SDK does not support SVG as a font.
But you could just convert the font, for example with http://www.freefontconverter.com/

Custom Font in ios not working

I want to use HelveticaNeue-UltraLight in my ios application. I'm adding the font file as a resource to my project and adding the "Fonts provided by application" key in the plist file. The file name is HelveticaNeue.dfont and I've added it to the key array.
When I check for the available fonts I can see it now...
NSArray *fonts = [UIFont fontNamesForFamilyName:#"Helvetica Neue"];
for(NSString *string in fonts){
NSLog(#"%#", string);
}
1-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-Bold
2011-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-CondensedBlack
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue-Medium
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-Light
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-CondensedBold
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-LightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLight // HERE IT IS!
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-BoldItalic
2011-07-08 17:32:57.870 myApp[5159:207] HelveticaNeue-Italic
But when I try to get it with [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:12] I just get HelveticaNeue-Light..
I'm getting no errors or warnings.
Help please!
Not only does the file needs to be added to the project, but it needs to be added to the target as well.
Sometimes (as in my case), when drag and dropping the ".ttf" file in Xcode, the "add to target" is not checked. This means the file is actually visible in your project, but it is not embedded in the app.
To make sure it is:
Click on the project's name (left pane)
Then on the target (middle pane)
Then "Build Phases" (third tab on the right pane)
Then "Copy Bundle Resources"
You should see the font file in that list.
I think you're having the same problem I had : there's a bug in iOS where it is not able to use more than 2 fonts from the same family.
See blog post here for details and solution : http://www.pixeldock.com/blog/uifont-problem-when-using-more-than-2-custom-fonts-of-the-same-font-family/
after adding Font file in Bundle
"Font Provided by Application" property click on drop down arrow
and enter your font name with extension
It is working with different variations of the same font(even in IOS 8). I'll post my mistake just in case someone has the same problem...
I was using font's filename instead of the font name.
A useful solution is print all the fonts available and look for the one I was using.
In Swift would be:
for fontFamilyNames in UIFont.familyNames {
for fontName in UIFont.fontNames(forFamilyName: fontFamilyNames) {
print("FONTNAME:\(fontName)")
}
}
I found this useful tutorial in: Code with Chris
Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.
It should work even for different font variations of the same family.
Drag your font files (.ttf or .otf) into project's bundle.
Then select all those fonts > on your xcode's right panel make sure the Target membership is enabled.
Select project from the navigator panel > select Build phases > under Copy Bundle Resources verify all your fonts are listed. If not, add one by one using + icon.
Info.plist > select Fonts provided by application > add your fonts one by one (e.g., Helvetica-Bold.ttf).
While this probably won't help the OP, it might be useful to others.
With iOS 5, Helvetica Neue is automatically included; you don't have to add this custom font to the bundle. It's even possible to use that font family directly from Interface Builder, which is extremely handy.
Try this this will be also heleful for you.
[compete_Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
After spending hours, I came to know that the Name for the font we need to add is not the file name of the font but the actual Font name. I was trying to include font file bonveno.ttf. I have included the filename along with extension in info.plist and was able to see the font in the Copy Bundle Resources list. My code was like
self.label.font = [UIFont fontWithName:#"bonveno" size:30];
that was causing the problem. Then I double clicked the font file from Finder to see the preview of the font. That time I noticed the name of the font in the preview window as BonvenoCF. So I added that name in the code like
self.label.font = [UIFont fontWithName:#"BonvenoCF" size:30];
Worked like charm!! Can't live without this cute font in my apps.
Try with spaces:
[UIFont fontWithName:#"Helvetica Neue UltraLight" size:12];
See Certain fonts not showing up?
I ended up buying the font Helvetica Neue UltraLight from a third party, added it with no problems.
In order to use custom fonts in iOS, just add your fonts by drag & drop to your project. Mention the array of font names with extension in Info.plist with key "Fonts provided by application". Then print all font family names using below code :
for (NSString* family in [UIFont familyNames])
{
DebugLog(#"FONT FAMILY: %#", family);
for (NSString *name in [UIFont fontNamesForFamilyName: family])
{
DebugLog(#" %#", name);
}
}
Console print :
Arial Hebrew
ArialHebrew-Bold
ArialHebrew-Light
ArialHebrew
Calibri
Calibri-Bold
Calibri
Georgia
Georgia-BoldItalic
Georgia-Bold
Georgia-Italic
Georgia
Then from console, you will get the actual font-names (As above). Use below code to create font -
In core text :
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
In UIKit :
UIFont *font = [UIFont fontWithName:fontName size:10];
Note : here fontName = #"Calibri-Bold" is without extension.
In addition to:
adding the font file names to the info.plist under "Fonts provided by
application"
adding the actual files to the project.
You also have to make sure that under Targets -> Build Phases -> Copy Bundle Resources has the actual filenames of the custom fonts.
To check if they have appeared, you can do something like:
UIFont.familyNames.forEach { (name) in
print(name)
}
Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation and open the font. If something is failing (even with a warning) in there iOS generally rejects loading the font in the app with no errors.
If this is the case then you can fix the errors by opening the font in a free app called "Typelight" And resaving the font "Save as".
In addition to the well explained #Ben answer, if you're still not getting your fonts, Just use or assign your font to any Label in one of your Storyboard, You will start getting it using this code
If you still unable to get font using [UIFont fontWithName:#"Font-Regular" size:11]; There must be problem in naming. Stop at any Breakpoint in debugger and Debug it using different names, and see which po prints object that is not nil
po [UIFont fontWithName:#"FontRegular" size:11];
or
po [UIFont fontWithName:#"Font Regular" size:11];
or
po [UIFont fontWithName:#"Font-Regular" size:11];
You will get <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt for actual font name otherwise you will get nil
note that add .ttf end of each Fonts provided by application name
It's an old question but I'd like to emphasize that we must write font file name WITH EXTENSION into info.plist.
It was particularly my mistake: I missed extensions. But the very interesting thing that iOS successfully loaded three fonts even when the extensions were missed.
Once I've added extensions all 12 fonts were loaded.
Good article:
https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

Certain fonts not showing up?

I have a list of font names in my app, which are all displayed in the font they represent. The following three do not work though:
ArialRoundedMTBold
ChalkboardSE-Bold
TrebuchetMS-Bold
Instead they display in the standard font.
Any ideas why these might not be showing up?
How to configure a custom font
To use a custom font (one not included in iOS) you have to edit your <appname>-Info.plist file and create a new UIAppFonts key with type array, where each element of the array is a String with the name of your font file. Example: VAGRoundedStd-Light.ttf. You also have to add the file to your project.
Note: When you type UIAppFonts and press enter, the key is converted to "Fonts provided by application".
However, when you use the font in Interface Builder (or with UIFont) you don't use the filename of the font, but the name that appears when you open the font in the application Font Book of your Mac. For the previous example it would be VAG Rounded Std Light.
OS X is more tolerant than iOS digesting TrueType formats, so on rare occasions you may find a font that works in OS X but not in iOS. If that happens, replace the font to see if at least you got the process right.
How to load a font programmatically
This solves the case where the font license requires you to distribute the font encrypted.
First step is to encrypt and decrypt the font with whatever algorithm you see fit.
Load the font as a NSData object and reverse the encryption.
Register the font programmatically.
This following recipe is from Loading iOS fonts dynamically by Marco Arment. It makes the fonts available in UIFont and UIWebView. The same code can be used to load fonts from the Internet.
NSData *inData = /* your decrypted font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error)
NSLog(#"Failed to load font: %#", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
How to load more than two fonts of the same family
This is the case where iOS refuses to load more than two fonts from the same family.
Here is a code workaround from stackoverflow user Evadne Wu. Simply stick the following in your app delegate (note that it uses two frameworks):
#import <CoreText/CoreText.h>
#import <MobileCoreServices/MobileCoreServices.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CTFontManagerRegisterFontsForURLs((__bridge CFArrayRef)((^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
NSArray *resourceURLs = [fileManager contentsOfDirectoryAtURL:resourceURL includingPropertiesForKeys:nil options:0 error:nil];
return [resourceURLs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL *url, NSDictionary *bindings) {
CFStringRef pathExtension = (__bridge CFStringRef)[url pathExtension];
NSArray *allIdentifiers = (__bridge_transfer NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, pathExtension, CFSTR("public.font"));
if (![allIdentifiers count]) {
return NO;
}
CFStringRef utType = (__bridge CFStringRef)[allIdentifiers lastObject];
return (!CFStringHasPrefix(utType, CFSTR("dyn.")) && UTTypeConformsTo(utType, CFSTR("public.font")));
}]];
})()), kCTFontManagerScopeProcess, nil);
return YES;
}
Available as gist. Commented in the author blog: Loading 2+ fonts on iOS from the same font family.
An alternative, more involved workaround from pixeldock.com:
If you add more than 2 font variants of the same font family (e.g.
“Normal”, “Bold” and “Extended”), only the last two font variants that
you added to the project will be usable.
If you see this happening, it is a limitation of your SDK version, and the only way out of it is editing the font family with a Font editor like Fontforge. Again, from pixeldock.com:
Open your Font in Fontforge
Goto ‘Element’ -> ‘Font Info’ and change the ‘Family Name’ field
Goto ‘Element’ -> ‘TTF Names’ and change the fields ‘Family’ and ‘Preferred Family’
Goto ‘File’ -> ‘Generate Fonts’ and save your edited font
Only a subset of Mac OS fonts are available in iOS. If you set a font that is not available, it will be displayed as the standard Helvetica.
I was using a Google font named Arvo. It had the following files:
Arvo-Regular.ttf
Arvo-Bold.ttf
Arvo-BoldItalic.ttf
Arvo-Italic.ttf
These names were added into my app's info.plist but for some reason, my app could only read the bold, bolditalic, and italic fonts. It couldn't read the regular font when i tried to load it. However, after printing out the font names, it came out that Arvo-Regular was recognized as Arvo in my app.
I had a similar issue with not finding Chalkduster - even though IB listed it as a font and it's listed as an IOS font. I discovered that the list of fonts on iPhone and iPad is not identical.
Chalkduster is available for iPad but not iPhone. Chalkboard is available on iPad 4.x but not 3.x. It is available on iPhone 3.x & 4.x.
More details here: http://www.whatsyourdigitaliq.com/2011/05/11/fonts-available-in-various-versions-of-ios/
Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.
It should work even for different font variations of the same family.

Resources