Cannot load specific icon from `SAP-icons.ttf` font - ios

I am trying to display SAP icon(please see link to the attachment) from custom font. I've added custom font SAP-icons.ttf to the project(it's included in bundle and loaded correctly, double checked), but I cannot load the correct symbol.
The code snippet that I use is below:
Get path to the font. It's stored in specific bundle. Get reference using CGDataProvider.
NSString *fontPath = [[NSBundle my_currentBundle] pathForResource:#"SAP-icons" ofType:#"ttf"];
CGDataProviderRef provider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
CFErrorRef error = nil;
if (! CTFontManagerRegisterGraphicsFont(fontRef, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(#"Failed to load font: %#", errorDescription);
CFRelease(errorDescription);
}
Get font name and create UIFont object.
NSString *fontName = (NSString *)CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
UIFont *font = [UIFont fontWithName:fontName size:20];
CFRelease(fontRef);
CFRelease(provider);
For desired icon I found &#xe202 referenced unicode in site with the link below:
https://sapui5.hana.ondemand.com/iconExplorer.html#/?tab=grid&search=info&icon=message-information
If &#xe202 unicode value is incorrect for this icon which should I use?
I can't event preview installed SAP-icons font characters in my Mac - all of them are question marks!
char *iconUnicodeStr = "&#xe202";
NSString *infoIconString = [NSString stringWithCString: iconUnicode encoding:(NSUnicodeStringEncoding)];
Apply specific font, set text. Result - incorrect symbol.
[self.button.titleLabel setFont:font];
[self.button.titleLabel setTitle:infoIconString];
Thanks in advance for any help and suggestions how could I do that!

Instead of
char *iconUnicodeStr = "U+xe202";
label.text = [NSString stringWithCString:iconUnicodeStr encoding:(NSUnicodeStringEncoding)];
Use
label.text = [NSString stringWithFormat:#"%C", (unichar)0xe202];
And not to forget set the font for the label
label.font = myCustomFont;

I am sure problem lies in the font name.
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
Run above code in AppDelegate didFinish and make sure you are using correct font name.
Search for SAP_icons in the NSLog from above code. If you don't find look for the proper name.
Search for SAP in above log, that will give you correct name.

Related

IOS: How to list an already installed custom font in my app

I am building an app for iPad and would like to use an already installed custom font in my ipad. I have downloaded a custom font (Cabin Sketch) from Adobe Creative Cloud Suite. After installing, I am able to see the font in General > Fonts
In my code, I am trying to print all the available fonts in the system but unable to see the one that's already been installed.
Here are the codes I have tried to print the font families.
NSDictionary *descriptorOptions = #{(id)kCTFontDownloadedAttribute : #YES}; // tried kCTFontDownloadableAttribute too
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)descriptorOptions);
CFArrayRef fontDescriptors = CTFontDescriptorCreateMatchingFontDescriptors(descriptor, NULL);
for(UIFontDescriptor *descriptor in (NSArray *)CFBridgingRelease(fontDescriptors)) {
NSString *fontFamilyName = [descriptor objectForKey:UIFontDescriptorFamilyAttribute];
NSLog(#"Font Family Name = %#", fontFamilyName);
}
NSArray *familyNames = [UIFont familyNames];
for (NSString *aFamilyName in familyNames) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:aFamilyName];
for (NSString *aFontName in fontNames) {
NSLog(#"%#", aFontName);
}
}
QCFType<CFArrayRef> familyNames = CTFontManagerCopyAvailableFontFamilyNames();
for (NSString *familyName in familyNames.as<const NSArray *>())
NSLog(#"%#", familyName);
Thanks in advance.

Set bold font to NSString iOS

I have imported 2 font files to my resources group in my project, and also added them to my plist file:
pt-sans.narrow-bold.ttf and PT_Sans-Narrow-Web-Bold.ttf
I want to set bold font to attributed string, but it can't find my fonts, i am getting this error:
(UIFont*) nil
This is how i am setting the font:
UIFont *font_bold=[UIFont fontWithName:#"PT Sans Narrow Bold" size:14.0f];
It needs to be font from this family. Any idea, what am i doing wrong?
You can check the font's are accessible by adding this to your didFinishLaunchingWithOptions
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);
}
and as #mikle94 said, caps is probably your issue.
In cases where we want to use fonts in our app, which don't already exist, we should pay attention to the several things, in order not to do the same mistake i did, and after that wonder "what is wrong, my code is perfectly written".
Copy the font in .otf or .ttf to your project.
Add it to the .plist file. Add 'Fonts provided by application' key in your plist file and in Item 0 copy the font name, which you already imported to your project.
Check in your Target -> Build Phases -> Copy Bundle Resources. If you don't see your font in there, add it.
Very Important - List the available fonts to check the correct names which you can use in your app:
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);
}
Find the font you need and set it this way:
UIFont *customFont = [UIFont fontWithName:#"PTSans-NarrowBold" size:20];

Custom Font not working in Xcode

I am trying to use a truetype (.ttf) font in my iOS app and it is not working. I have added the font to my project and added the target correctly. I added the font in the info.plist under fonts provided by application. I am using an NSAttributedString to display text and this is my code
UIFont *hrFont = [UIFont fontWithName:#"ocrb" size:18.0];
NSDictionary *hrDict = [NSDictionary dictionaryWithObject:hrFont forKey:NSFontAttributeName];
NSMutableAttributedString *hrAttrString = [[NSMutableAttributedString alloc] initWithString:hrString attributes: hrDict];
UIFont *barcodeFont = [UIFont fontWithName:#"IDAutomation DataBar 34" size:22];
NSDictionary *barcodeDict = [NSDictionary dictionaryWithObject:barcodeFont forKey:NSFontAttributeName];
NSMutableAttributedString *barcodeAttrString = [[NSMutableAttributedString alloc]initWithString: alphaOnly attributes:barcodeDict];
[hrAttrString appendAttributedString:barcodeAttrString];
The barcode font displays correctly but the ocr font displays as the system font instead. Any help would be greatly appreciated.
Add your custom font into your project , i.e. Dragged the font
file(ocrb.TTF) into XCode project.
Edit Info.plist: Add a new entry with the key "Fonts provided by
application".
For each of your files, add the file name to this array
Opened the font in font book(double click on your font in finder) to
see what the real filename is and I see this:
Now set font to your label
yourLabel.font = [UIFont fontWithName:#"ocrb" size:15];
You can check whether that font is loaded or not by using the following 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);
}
If your font is not loaded then download new font & add it into your project. After downloding & adding into project works for me.

What is the correct way to use a custom font in IOS?

I have been trying to use a custom font[1] in my game and I have managed to use it in Android and web but not in IOS.
I have my xcode project like this:
http://discuss.cocos2d-x.org/uploads/default/9079/0e7bfa08016642fa.png
But I have tried writing only Soup of Justice in xcode but that did not work.
In my cocos studio project I have in resource.js:
font: 'res/Fonts/Soup of Justice.ttf' and it's preloaded in the main.js
Then my calls of cc.LabelTTF :
var label = new cc.LabelTTF("Prueba" , "Soup of Justice", 50);
but i have also tried
var label = new cc.LabelTTF("Prueba" , 'res/Fonts/Soup of Justice.ttf', 50);
Any help would be welcome!
[1] http://www.dafont.com/es/soup-of-justice.font
This is the link that should help you, and get you on roll...
Add the font to your project
Include the names of the fonts in your PList under key under key "Fonts provided by application"
Use the Fonts with correct name.
Use the code written below to print all the fonts and search for the one you want to use.
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
Hello have you checked the fonts in your app 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);
}
This small construct will list all the fonts that can be used in your app along with the fonts added in project by you, the name displayed there would the name of font which can be used

Custom built font displays properly when set in IB but not when set programmatically

I have a custom built font that I've added to the Xcode project. I've followed all the instructed steps in adding the font and when I set a UILabel or UITextField as the custom font in IB it works great, however when I set the custom font at runtime it appears as the iOS default font instead. I've checked the UIAppFonts list and have them print out with NSLog where it shows the full name so I know that when I call [UIFont fontWithName:#"font name" size:s] it's the correct name. What could be the issue?
The font name finding method below (This method provided the incorrect names to use in custom UIFont instantiation):
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:#"UIAppFonts"];
for (NSString *fontFile in fontFiles) {
NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
NSData *fontData = [NSData dataWithContentsOfURL:url];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
CGFontRelease(loadedFont);
CGDataProviderRelease(fontDataProvider);
NSLog(#"FULL NAME:%#",fullName);
}
Try executing this code:
for (NSString* family in [UIFont familyNames]) {
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family]) {
NSLog(#" %#", name);
}
}
Do you see the font name you are trying to use?

Resources