Wednesday, December 17, 2014

Using Custom Fonts in Android - Delphi

Android

For XE7  to 10.2.1 Tokyo

 You should be able to use any True Type Font in an android app.

Example using the font Script MT Bold. This font is in the Windows Font Folder. The font file name is SCRIPTBL.TTF.

1. copy SCRIPTBL.TTF to the project folder.
2. In Deployment window, add SCRIPTBL.TTF file. Set Remote Path to .\assets\internal\
Set Remote Name to .SCRIPTBL.ttf (make the ttf extension lower case so we can hard code it into the program)



3. Load FMX.FontGlyphs.Android.pas into the editor. ( the file is in C:\Program Files (x86)\Embarcadero\Studio\15.0\source\fmx)
If it is read only, right click in editor and turn off Read-Only.
Save the file to the project folder. Don't change the file name. Then you can edit it in the Delphi editor.
  A. Add System.IOUtils to the uses clause.
  B. Go to procedure TAndroidFontGlyphManager.LoadResource;
  • Add a var FontFile: string;
  • There is a line in the procedure that says:Typeface := TJTypeface.JavaClass.create(FamilyName, TypefaceFlag);
  • Replace that line with:
 FontFile := TPath.GetDocumentsPath + PathDelim + CurrentSettings.Family + '.ttf';
 if FileExists(FontFile) then
   Typeface := TJTypeface.JavaClass.createFromFile(StringToJString(FontFile))
   else
     Typeface := TJTypeface.JavaClass.Create(FamilyName, TypefaceFlag); 
This will make it look for a font file by that name first.

4. For each component you want to use this font on, set the TextSettings Font Family property to SCRIPTBL
This is the name of the font file without the file extension. Don't put "SCRIPTBL.ttf" and don't put "Script MT Bold".
You will have to type or paste it in to the property box.

Note: filenames in Android are case sensitive, so you have to get them all the same.

This is a form with label, textbox, memo, checkbox, radio button:
Note: an easier idea for the filename is:
If the font family name is not the same as the file name of the font, you can just make a copy of the font file and rename it to the Font FamilyName and use that. Then the font will show up at both design time and runtime.
 

How to Use Custom Fonts in IOS - Delphi


IOS

XE7

You should be able to use any True Type Font in your IOS app.

Example using the font Script MT Bold. This font is in the Windows Font Folder. The font file name is SCRIPTBL.TTF.

1. Copy SCRIPTBL.TTF to the project folder.
2. Compile the app for IOS so that it makes an info.plist file in the debug folder.
3. Open the info.plist file in the Delphi editor. Save it as custom.info.plist in the project folder.
Add another key in it:
 <key>UIAppFonts</key>
 <array>
 <string>SCRIPTBL.TTF</string>
 </array>

This tells IOS to be able to use the additional font file. IOS will automatically look for it in the bundle.

4. In the Deployment page, add SCRIPTBL.TTF with Remote Path of .\



5. Add your custom plist file with Remote Name of info.plist



6. Uncheck the deployment of the old info.plist



This will substitute the info plist with your font info in it.

For each component, set the TextSettins Font Family property to Script MT Bold. Since this font is already installed in Windows, you can choose it off the dropdown list.

This is a form with label, textbox, memo, checkbox, radio button.