1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Java Question Fonts & Typeface.createFromAsset

Discussion in 'Libraries developers questions' started by tchart, Apr 7, 2015.

  1. tchart

    tchart Active Member Licensed User

    All, another question about wrapping libraries with resources. This one has to do with fonts.

    The library Im trying to wrap has the following code in it;

    Code:
    Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s.ttf", name));
    I get an error stating that it cant find the font file. The problem from what I can see is that the library Java code is expecting the font to be in the "fonts" sub-folder. I've tried altering the code to look in the root resources folder but that didnt work either.

    In the end I altered the code to be this, which works;

    Code:
    Typeface t = Typeface.createFromAsset(BA.applicationContext.getAssets(), String.format("%s.ttf", name).toLowerCase(BA.cul));
    This relies on B4A to create the font which is fine but I would like to remove the need for me to alter any code from the original library source code.

    Is there a trick I need to know about loading the font correctly?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Have you copied the font file to a folder named fonts under the project Files folder?
     
  3. tchart

    tchart Active Member Licensed User

    Thanks Erel, yes I tried that too. I think maybe the context under which it tries to load the fonts must be different or something.
     
  4. DonManfred

    DonManfred Expert Licensed User

    it should work if the font file in assets (or in assets/fonts/) is lowercased.
     
  5. tchart

    tchart Active Member Licensed User

    Thanks Manfred, I will give that a try.
     
  6. tchart

    tchart Active Member Licensed User

    Manfred, you are a legend.

    Added fonts to ..\files\fonts folder and altered code to lowercase the font name.

    Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s.ttf", name).toLowerCase());
     
    DonManfred likes this.
  7. DonManfred

    DonManfred Expert Licensed User

    What i meant to say was that you should copy only lowercased filenames into the assets (and it´s subfolders) instead of
    doing a
    Code:
    .toLowerCase()
    as it is not needed to change the code if folder and filenames are lowercase. Your goal was to suppress the needs to change the original code or not?
     
  8. tchart

    tchart Active Member Licensed User

    DonManfred b4a lower cases the font file names. The problem is actually that library developer has hard coded the font names in mixed case. So I have to lower case them in his helper function. I've asked him if he can lower case the font names but he said no.
     
  9. DonManfred

    DonManfred Expert Licensed User

    I understand
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice