Adding custom font to project, programmatically add item to plist - objective-c

I want to add some font to my project. I have directory in my project contains fonts. So i can access to my font by:
NSString* path = [[NSBundle mainBundle] pathForResource:#"res_name" ofType:#"res_type" inDirectory:#"res_directory"];
But how to add it to my project so i will be able to call it like this:
[UIFont fontWithName:#"Font-Name" size:14.0]
I'm only interest how to do this programmatically I'm assume that I have to add it to my plist, but can i do this programmatically?
I understand o have to use a in plist:
<key>UIAppFonts</key>
<array>
<string>swellfont.ttf</string>
</array>
But i cannot do this in Xcode manually. I want to check what fonts i included in my project in resource directory and add them programmatically from code.

The UIAppFonts key in Info.plist.
You can take a look at the Core Text Font Manager to see if registering fonts by URL make them loadable with +[UIFont fontWithName:].

Related

File icons getting changed to App icon in macOS Catalina

I have a Mac app that opens office files(.doc, .xls, etc.) and I need to show custom icon for these supported files.
I used to achieve by adding the supported UTI types in CFBundleDocumentTypes and assign my custom icns icon. Also set my app as the default app which opens these file types.
Now this approach worked flawlessly until macOS Catalina beta, even with Microsoft apps being present along with my app. In macOS Catalina beta onwards, I am seeing my app icon in place of all the file icons.
I tried clearing icon cache and even relaunching Finder, but to no avail.
Later on I even tried by adding the UTI types under UTExportedTypeDeclarations and UTImportedTypeDeclarations.
Is this a bug with Catalina Beta? Or anything I can do to get this working.
My plist has UTExportedTypeDeclarations looking like this. UTImportedTypeDeclarations and CFBundleDocumentTypes have similar values.
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>org.openxmlformats.spreadsheetml.sheet</string>
<string>org.openxmlformats.openxml</string>
<string>public.composite-content</string>
</array>
<key>UTTypeDescription</key>
<string>Excel Open XML spreadsheet</string>
<key>UTTypeIconFile</key>
<string>custom.icns</string>
<key>UTTypeIdentifier</key>
<string>com.microsoft.excel.openxmlformats.spreadsheetml.sheet</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>com.apple.ostype</key>
<string>XLSX</string>
<key>public.filename-extension</key>
<array>
<string>xlsx</string>
</array>
</dict>
</dict>
</array>
It looks like macOS 10.15 changed the way how the corresponding icon for a type is resolved.
I found a way how to get proper document icons for my app on Catalina:
Previously I only had an icon defined for the CFBundleTypeIconFile key in my CFBundleDocumentTypes dictionaries. But since Catalina, the system uses the LSItemContentTypes array to find an exported UTI and then uses the icon of that UTI (defined via the UTTypeIconFile).
If the LSItemContentTypes array contains types that are not known to the system yet (= newly introduced custom types), those types must also be exported by defining dictionaries in UTExportedTypeDeclarations.
This works for my case because I use a custom file format with its own UTI.
For the case you are describing, where existing UTIs are used, I am not sure if it is still possible to override the icons of UTIs that you don't "own".
I think the correct way to define custom icons for 3rd party types would be to define dictionaries in the imported types array (UTImportedTypeDeclarations). In that case the system should choose your custom icon as long as no other app declares ownership of an UTI by exporting it. I suppose that the icon of the app that is set as Default application for a type wins in the case where 2 or more apps claim ownership for it (Haven't tried that).
Another issue I ran into was, that the icon file assigned to UTTypeIconFile can't come from an Asset Catalog (this worked for CFBundleTypeIconFile). It needs to refer to an .icns file in the Resource folder in your bundle.
Existing .iconset folders from the Asset Catalog can be converted to icns with the following command:
iconutil --convert icns iconname.iconset
I am not sure if this intended behavior or if this is just a bug in the Asset Catalog compiler build phase of Xcode 11. I filed a bug via Feedback Assistant.
I also have this issue. Tried empty project and reproduced it with bundled macOS applications, so it's mostly safe to assume this is a bug in macOS Catalina.
As trojanfoe suggested in a comment the only thing to do right now is reporting it to Apple via the Feedback Assistant.
Thomas Zoechling refers to another issue that may affect earlier macOS versions too, but does not solve the main issue here.

Labels are not changing to custom font

I am trying to set a label to a custom font with the following code.
cell.lblDataum.font = [UIFont fontWithName:#"Homenaje_Regular" size:10.0];
I'v added this font in my .plist.
<key>UIAppFonts</key>
<array>
<string>Homenaje_Regular.ttf</string>
</array>
But still no succes. Can anybody help me?
Kind regards
Are you sure you added you Homenaje_Regular.ttf file to your Xcode project and added it to your target's Copy File Build Rule (checkbox is checked next to the target name in the Inspector Panel?) so that the TTF file is copied into the application bundle on compilation.
But more likely, the name of the font is probably not #"Homenaje_Regular". You have to use the real font name as a parameter for fontWithName:and that's not always the same as the file name.
In your case, the real name of your font is probably "Homenaje-Regular" or just "Homenaje" and not "Homenaje_Regular".
You can have the real name of the font by opening your font in FontBook on your Mac and simply press Cmd-I to get that information.
You can also list by code in your iOS app all the fonts available to make sure your font is correctly detected by your app and pick up the right name of your custom font to use with fontWithName: :
for ( NSString *familyName in [UIFont familyNames] )
{
NSLog(#"=== Family %# ===", familyName);
NSLog(#"Names = %#", [UIFont fontNamesForFamilyName:familyName]);
}
I believe, the name of the font is Homenaje-Regular, not the file name.

Custom font doesn't work

I'm trying to add a font into my project to use in a label but it just don't work. Here is what I'm doing:
I copy the font file into my project resources.
Set my plist file with the array key UIAppFonts.
Add my font's name Kingashandwriting.ttf (the real name with the extension
, not the file name) into the array.
Set up my font in the code: UIFont *font= [UIFont fontWithName:#"Kingashandwriting" size:15];
Set it into my label: [self.myLabel setFont:font];
myLabel is an IBOutlet that is linked with a label in interface builder.
I'm using Xcode 4.3.3
Am i missing something?
I remember that I ran into this same problem when implementing custom fonts into my application. I thought that I had the official name right but it was giving me problems.
My advice is to open up the file in font book and look at the top of the window and see what that name says. Then format it exactly how you see it in your fontWithName: method.
Here's an image of what you're looking for:
http://i.stack.imgur.com/uOZy6.png
Then your font method would look like this (based on the font in the image):
UIFont *font= [UIFont fontWithName:#"BankGothic Lt BT" size:15];
For further information, in my application my file is named bankgthl.ttf and I have that file name added into my plist array. But when I am setting the font in my code I call it the same as above using the name seen in font book and it is all working fine.

NSLocalizedString doesn't work

I want to use NSLocalizedString in my app but it always failed. What i do is:
Define 3 Localizations in Project Properties (See screenshot bellow)
Create a new file: Resource Strings File
Check in the app bundle if file.strings is there
Then I use NSLocalizedStrings as follow but it doesn't work!
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSString *v1 = NSLocalizedString(#"MyWindow", nil);
//NSString *v1 = [[NSBundle mainBundle] localizedStringForKey:(#"MyWindow") value:#"" table:nil];
[label setStringValue:v1];
}
In my 3 .strings files I define the below key/value:
"MyWindow" = "Ma Fenetre";
Normally, my label should display "Ma Fenetre" and not "MyWindows"
You can download an example project here and tell me where is the issue.
Bellow the content of Resources folder in my app bundle :
DerivedData om$ find test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//MainMenu.nib
Thanks
Elfoiros
NSLocalizedString uses Localizable.strings file by default. Change your File.strings name and try again.
For every time you make changes in .Strings file you need to clean your project and remove application from device and simulator. This is the only way to develop Localised application.
Have a happy coding.!
As Adam stated, NSLocalizedString uses Localizable.strings for a lookup table. To specify a custom table, use:
NSLocalizedStringFromTable(#"MyWindow", #"File");
Documentation
Need to take care over case sensitive file names: Localizable.strings not localizable.strings. Simply rename in Finder, delete reference in Xcode project and add Localizable string back to the project. Then make sure the appropriate Localization boxes are selected in the file inspector. Don't know if there's a better slicker process.
I had a similar issue as described here but the solution was different. Somehow I managed to mangle how the Localization.strings file was being referenced in the project.
Removing the Localizable.strings file and readding it fixed the problem for me.

iPhone Development: how to use custom fonts?

I used a special font in my photoshop design,
is it possible to use these fonts natively in the iphone app? or
do I need to convert them into an image first?
If this is the only way to do it, what do you do with dynamic text?
Copy your font file into Resources
In your application .plist create (if it's exist just create a row) a row called "Fonts provided by application" and then in "item 0" copy your font name for example "Ciutadella-Bold.otf" (if there are some spaces in font name, rename it and for example replace all spaces to minus '-')
Then you can define this font in your application:
UIFont *CiutadellaBold = [UIFont fontWithName:#"Ciutadella-Bold" size:17.0f];
And use in for instance in uiLabel:
[uiLabel setFont:CiutadellaBold];
UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Where "MyFont" would be a TrueType or OpenType file in your project (sans the file extension), and label would be an instance of UILabel.
Yes it is possible with iOS 3.2 and later. You need to have the fonts file, I forget what formats exactly. Add the font file as a resource to your project then add the names of the fonts in the applications info.plist in an array under the raw key 'UIAppFonts' or under the friendly key 'Fonts provided by application'.
Here's a sample application that you can look at - https://files.me.com/tobiasoleary/ey08n1. It prints out the all the fonts accessible to the application. Two fonts have been added Flames and Firestarter.
This is by far the easiest way to add custom fonts to application. If you need to support custom fonts for iOS before 4.0 see http://github.com/zynga/FontLabel/tree/master
This question has been asked before here: Can I embed a custom font in an iPhone application?.
Note :
The parameter of “fontWithName” must be the real name of the font, not the name of the file. If you open your ttf file with the Mac Font Book, you will directly see its name on top of the window.