Does iOS SDK support SVG as custom font? - ios

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/

Related

How to use weather fonts in iOS SDK

I am developing for both android and iOS. I have an weather API in which I have to show a particular icon based on climate condition. I am using Weather.ttf fonts to do so.
In android I am doing like this :
<string name="weather_sunny"></string>
<string name="weather_clear_night"></string>
<string name="weather_foggy"></string>
<string name="weather_cloudy"></string>
And using the name I can get the Image.
But when In iOS I assign the text  to UILabel I am getting this text as output. How can use this to get a Icon.
You have to add your ttf to your project and, in your project.plist (Info.plist by default), you have to add an entry called "Fonts provided by application". Inside this entry, you have to add the name of your ttf files.
NOTE: This don't allow you to set this font on Interface Builder. You have to set it manually by code.
Example: [label setFont:[UIFont fontWithName:#"name_of_your_font" size:12.0]];
UPDATE
And then, try to set text with a stringWithUTF8String with "\xEF\x80\x8D".
UPDATE 2
The name of the font is "WeatherIcons-Regular". And according to the value, you have to set it as a simple string with '\u' before the value:
[label setFont:[UIFont fontWithName:#"WeatherIcons-Regular" size:12.0]];
[label setText:#"\uF00D"];
And that's all. I've tested it and it works.
Finally After Day of efforts I come to this solution.
To use weather fonts in iOS SDK you need Weather.ttf font file.
The code I was using to show Icons is  which is HTML Entity thats why I was not able to render it.
So I use this link to convert it in UTF String the I used that code.
Please make sure you are using correct font Name.To find Correct font name I use this code.
NSArray * fontsArray = [UIFont familyNames];//this gives array of all fonts
NSArray *newArray = [UIFont fontNamesForFamilyName:#"Weather Icons"];//Weather Ions must be listed in fontsArray so that you get all details in newArray
Now you have to just set the font and UTF code to UILabel
[lblCloud setFont:[UIFont fontWithName:#"WeatherIcons-Regular" size:14.0]];
lblCloud.text = [NSString stringWithUTF8String:"\uf014"];

Can't get custom font to load in iOS app

I know this question has been asked before, but so far have not found an answer to my problem. I am trying to use a custom font (Rockwell) in my app, but can't get it to load in.
I have used the correct name from Font Book, which is just "Rockwell". I've added the file to the project, and its target is the project. I've added the file to the plist, and also to the bundle resources.
I'm using the following code to set the font of a label, with no luck.
UIFont *customFont = [UIFont fontWithName:#"Rockwell" size:18.0];
[self._textView setFont:customFont];
It also doesn't appear when I run something like:
NSLog(#"Available fonts: %#", [UIFont familyNames]);
What am I missing?
Downloaded .ttf file from a different source, and now it works.

fontWithName:size: How to import own font to project?

I try to import my own font to a project and i'm stuck. I do my research and found this
but As of iOS 3.2, this functionality is built in. If you need to support pre-3.2, you can still use this solution. and I create app for iOS 5.0.
I'm try to use fontWithName:size: and docs said that The fully specified name of the font. This name incorporates both the font family name and the specific style information for the font. So I Put there my display name.
cell.textLabel.font = [UIFont fontWithName:#"Sansation-Light" size:10.0];
But it doesn't work. Where do i have store this font? In my project? is that correct name? My font name is: Sansation_Light.ttf
For This,
You have to add key "Fonts provided by application" in your info.plist .
for that key add your ttf file name for ex: "Sansation_Light.ttf" .
it works fine for me.
This works!
http://kgriff.posterous.com/45359635
It seems to be the line between Sansation and Light, it should be
cell.textLabel.font = [UIFont fontWithName:#"Sansation_Light" size:10.0];
Or change your font name to Sansation-Light.ttf

Adding a UIFont and looking for the fontfamily crashes with EXC_BAD_ACCESS

My problem is, that when I add a font which is described here and when I want to retrieve the font family names with [UIFont familyNames] it crashes. I did it the same way described in the example, but for me it doesn't work.
This is my stack trace:
0x314b9ebe in CFDictionaryGetValue
0x33d79be4 in copy_localized_value
0x33d79bd4 in CGFontNameTableCopyRootName
0x33d796f2 in CGFontNameTableCreate
0x33d79e86 in CGFontCopyFamilyName
0x3414ca00 in AddFontsFromCGFontAndPath
0x3414cb7e in AddFontsFromURLOrPath
0x3414cc98 in Initialize
0x3414ce0a in GSFontCopyFamilyNames
0x323fce84 in +[UIFont familyNames]
...
I'm testing it on iOS 3.2.2 and because i don't want my iPad to be upgraded, I can't tell if it works on other iOS Versions
What am I doing wrong?
Are you sure your font file is valid and works?

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/

Resources