Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18291] Android: Feature Request to Expose Typeface.createFromFile to dynamically include the fonts

GitHub Issuen/a
TypeNew Feature
PriorityNone
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labels2019-cl
ReporterEduardo Gomez
AssigneeUnknown
Created2014-12-26T20:22:27.000+0000
Updated2019-03-04T21:51:08.000+0000

Description

Feature Request

Create a font form from file which is really required to load the dynamic font downloaded from our server or at least if the mCustomTypeFaces variable is declared as public.

Android API

Create a new typeface from the specified font file. http://developer.android.com/reference/android/graphics/Typeface.html#createFromFile(java.lang.String)

Use Case

Our requirement is to download the fonts (TTF) files form server and put them inside app directory, then use it. So if we like to change the fonts tomorrow after app release, we don't have to make a new release.

Comments

  1. Manoj Kumar 2015-01-13

    It is doable on iOS too - http://stackoverflow.com/questions/14048549/how-to-dynamically-load-a-font-under-ios-for-real
  2. Joshua Quick 2018-01-30

    I see a much larger issue here. Currently, Titanium only supports loading fonts by "family name". But loading by font family name is not portable between different platforms (ie: Android, iOS, Windows). Even from the same font file. In fact, our sample code on the following guide shows this exact problem and how to deal with it... http://docs.appcelerator.com/platform/latest/#!/guide/Custom_Fonts Also, it's a bit of a guessing game to figure out which name to load per platform, which can cause tech-support issues. Tt can take several builds of guessing at the name to figure it out (very inconvenient and tedious). The following guide indicates this exact problem... http://docs.appcelerator.com/platform/latest/#!/guide/Custom_Fonts-section-src-29004935_safe-id-Q3VzdG9tRm9udHMtRmluZGluZ2Fmb250J3NQb3N0U2NyaXB0bmFtZQ The best and most portable solution is to offer the ability to load fonts by file name (just as easy as loading image files; no guide required). This should be favored over loading them by family name due to the issues mentioned above. So, I recommend that our "font{}" dictionary also support a "fontFilePath" field. Titanium should then select the first font found within the font file, but typically TTF and OTF font files only contain one font within them. If there are multiple fonts contained in the file, then the "fontFamilyName" can be used to select it.
       Ti.UI.createLabel(
       {
       	text: "Hello World",
       	font:
       	{
       		size: 20,
       		fontFilePath: "/assets/fonts/MyCustomFont.ttf",
       	},
       });
       
    I think a separate JIRA case should be written for the above. [~hknoechel], [~kiguchi], what do you think? (I've done the above in a previous SDK I used to work on and found that this method worked best.) Also, [~kiguchi], we may need to double check if there is a WinRT API for fetching family names within a font file. Back when I worked on Windows Phone 8.0 Silverlight, there was no API to do this and I had to load a font by "#", where the "" part was a required field. Is there now a WinRT API for doing this?
  3. Kota Iguchi 2018-01-31

    I would think that we might want to use path instead of fontFilePath, it may sound a bit redundant.
       Ti.UI.createLabel(
       {
       	text: "Hello World",
       	font:
       	{
       		size: 20,
       		path: "/assets/fonts/MyCustomFont.ttf",
       	},
       });
       
    Or, we could follow how Microsoft does it...On Windows, you can use \[FontFilePath]#\[FontName] format for fontFamily property like below. (TIMOB-24299)
       Ti.UI.createLabel({
           text: 'Hello World, Bold',
           font: {
               fontSize: 36,
               fontFamily: 'segoeuib.ttf#Segoe UI 8'
           }
       });
       
    Maybe we could introduce same format for fontFamily on other platforms?
  4. Joshua Quick 2018-01-31

    [~kiguchi], thanks for your feedback. The advantage of having a separate font "path" field is so that it can also be assigned a file object returned by our Ti.Filesystem API. We currently have a customer who is interested in loading downloaded fonts and this would enable them to do so.

JSON Source