Custom Font not working in Xcode - ios

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.

Related

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

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.

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];

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

Setting custom font in UITextView with attributed string

I have a UITextView which contains an attributed string configured through storyboard. Now i want a custom font to be applied. I am doing it through code
textView.font = [UIFont fontWithName:kCustomFont size:textView.font.pointSize];
The problem is fonts get changed losing all the attributes. How do i fix it?
(Working on iOS 5 and above)
What's in your kCustomFont? If the FontName is wrong, even a single Character, the standard SystemFont will be used.
You should change it via NSMutableAttributedString's attributes.
I.e. use something like:
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.text];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
[mutableSrting addAttributes:attrs range:NSMakeRange(0, [mutableString length])];
textView.attributedText = mutableString;
[mutableString release];

How to make bold font NSString value?

In my iOS application, how can I make bold font in NSString value? I used below code for making bold font text in NSString value, but showing error
like this:
Undefined symbols for architecture i386:
"_kCIAttributeName",
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:#"%#%#%#",key,#":",#" "];
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCIAttributeName value:boldFontName range:boldedRange];
[attrString endEditing];
NSString *string = [NSString stringWithFormat:#"%#%#",attrString,[dic objectForKey:key]];
How do I make bold font in string?
the NSString is for store a simple texts without any font formatting.
the UILabel has a font property, you can use it to set the visible text's font on the UI, but the UILabel can work with one format only and the whole text gets the same font and style.
if you really want to use a kind of rich text document, you should create a UIWebView class and you can show any document inside with the standard HTML tags.
UPDATE
the UILabel can store attributed string since iOS6, which gives you a chance to show rich-formatted text in one single UILabel instance, even using various fonts or sizes, etc...

Resources