Set bold font to NSString iOS - 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];

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.

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.

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

How to load only Custom font names in Array?

I am using Multiple custom fonts in my app.i know how to Add custom font but the Terrible parts of all this process is when we are going to use the custom font because mostly the custom font name is different from the file that we are using in app.here i would like to explain the recent example that i did,First of all i have added two custom font file in my app
For getting the above custom font names i tried this way
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
FontnameArray=[[NSMutableArray alloc] init];
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSString *fullName = [NSString stringWithFormat:#"%#",[fontNames objectAtIndex:indFont]];
[FontnameArray addObject:fullName];
}
}
[FontnameArray sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#" Font name: %#", FontnameArray);
The above Code display 199 font names for me ,so it was to difficult for me to pick the custom font name among 199 fonts, To make my doubt clear then i have simply drag the font file to font book Then i got the orignal names of custom Font.
[UIFont fontWithName:#"Ethiopia Primary" size:20]];
[UIFont fontWithName:#"Ethiopia jiret" size:20]];
So the Question is how can i get all Custom font names in Array like Above instead of draging the each font file to Fontbook for getting orignal names.
Just compare with all fonts exist in iOS
*Create new project like this code and write array of all font names to a plist file
- (void)writeAlliOSFontsToPlist{
NSArray *fontFamilies = [UIFont familyNames];
[self savePlist:#"AllFont" fromArray:fontFamilies];
}
- (NSString *) getFilePathForPlist:(NSString *) plistName {
// placeholder
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",plistName]];
}
-(void) savePlist:(NSString *)plistName fromArray:(NSArray *)array {
[array writeToFile:[self getFilePathForPlist:plistName] atomically:YES];
}
Open your simulator dir and copy this plist to your App bundle.
Then read this plist to an array and compare with all of your fonts by this code
-(void)dumpCustomFonts{
NSArray *iOSFontsArray = [NSArray arrayWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"AllFont.plist"]];
NSMutableArray *fontFamilies = (NSMutableArray *)[UIFont familyNames];
[fontFamilies removeObjectsInArray:iOSFontsArray];
NSLog(#"%#",fontFamilies);
}
I've done all in my test project, you just copy and paste it.
You should add your custom font in .plist by
1) Add a new entry with the key "Fonts provided by application".
2) Now add each custom font file name with extension to this array like :
Now you can retrive all your custom fonts by :
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
fontFamilyNames = [plistData valueForKey:#"UIAppFonts"];
NSLog(#"%#",fontFamilyNames);
NSString *fontFamilyName = [[fontFamilyNames objectAtIndex:index] stringByDeletingPathExtension];
UILabel *fontNameLabel = [[[UILabel alloc] init] autorelease];
fontNameLabel.text = #"Preview Text";
fontNameLabel.font = [UIFont fontWithName:fontFamilyName size:20.0];

Resources