How do you integrate custom fonts in an app? - android

I am trying to integrate custom fonts (Imperator.ttf and goodfish.ttf) from the following website into my corona app:
http://www.1001freefonts.com/top-fonts.php
I am following carefully the tutorial found here on Corona's official website:
http://www.coronalabs.com/blog/2013/01/16/faq-wednesday-custom-fonts/
I follow the whole procedure for macosx integration (installing the font by double clicking etc) and do not get any error. I also added the font file name to my build.settings file:
UIAppFonts =
{
"Imperator.ttf"
},
Whenever i launch the simulator and debug the currently loaded fonts, the new one doesn't appear.
Is there an easy way to debug the issue on corona simulator, on osx, to troubleshoot why the font doesn't load ?
Thanks

While installing custom fonts to your corona application, you have to follow the following steps:
Install the font in your system.
Copy and paste the font file(such as: Imperator.ttf) to the application folder where your main.lua exists.
Then for iPhone(only for iPhone, Android doesn't need this), add the following lines to your build.settings:
iphone =
{
plist =
{
UIAppFonts =
{
"Imperator.ttf" -- Font file name
},
UIApplicationExitsOnSuspend = true
},
}
Debug and get the supported Font Names by the system using the following:
local fonts = native.getFontNames()
for i,fontname in ipairs(fonts) do
print(fonts[i])
end
From the above debugging, you can get the exact Font Name (here: Imperator) that you have to use while creating the text/label. Sometimes this may differ from the name of the font file. You can also get it from applications like photoshop(it's text tool font name), etc.
local myText = display.newText("Label with custom font",150,100,"Imperator",20)
Then finally, you will get the output as:
Keep coding................ 😃

Put Imperator.ttf font file in Assets folder
In class use following code:
Typeface imperator_typeface =Typeface.createFromAsset(getContext().getAssets(),"Imperator.ttf");
your_textview.setTypeface(imperator_typeface);

Related

How to correctly load XML files on mobile devices using unity3d

I have some level definitions in xml format (with .txt extension) inside (without any subfolders) my project's rescources folder
I for more scalability, I have a plain text file naming all these level definition XMLs
I read that file using
TextAsset WorldList = (TextAsset)Resources.Load("WorldList");
And then I load the needed world:
TextAsset TA = (TextAsset)Resources.Load(/*"LevelDefs/" +*/ Worlds[worldToload]);
xps.parseXml(TA.text);
loadLevel(levelToLoad);
(you see that I have moved these rescources out of subfolder to reduce the chance of them not loading)
worldToload here is the index number of that world
program works fine on windows but nothing loads on my android test device.
I seem to have problems debugging on device so I only guess something went wrong in loading phase.
any suggestions?
From Unity Documentation:
Most assets in Unity are combined into the project when it is built.
However, it is sometimes useful to place files into the normal
filesystem on the target machine to make them accessible via a
pathname. An example of this is the deployment of a movie file on iOS
devices; the original movie file must be available from a location in
the filesystem to be played by the PlayMovie function.
Any files placed in a folder called StreamingAssets in a Unity project
will be copied verbatim to a particular folder on the target machine.
You can retrieve the folder using the Application.streamingAssetsPath
property. It’s always best to use Application.streamingAssetsPath to
get the location of the StreamingAssets folder, it will always point
to the correct location on the platform where the application is
running.
So below snippet should work:
// Put your file to "YOUR_UNITY_PROJ/Assets/StreamingAssets"
// example: "YOUR_UNITY_PROJ/Assets/StreamingAssets/Your.xml"
if (Application.platform == RuntimePlatform.Android)
{
// Android
string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");
// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while ( ! reader.isDone) {}
realPath = Application.persistentDataPath + "/Your"; // no extension ".xml"
var contents = System.IO.File.ReadAll(realPath);
}
else // e.g. iOS
{
dbPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");
}
Thanks to David I have resolved this problem.
for more precision I add some small details:
1-As David points out in his answer (and as stated in unity documentations), we should use StreamingAssets if we want to have the same folder structure. One should use Application.streamingAssetsPathto retrieve the path.
However, files are still in the apk package in android's case, so they should be accessed using unity android's www class.
a good practice here is to create a method to read the file (even mandatory if you are going to read more than one file)
here is one such method:
private IEnumerator LoadDataFromResources(string v)
{
WWW fileInfo = new WWW(v);
yield return fileInfo;
if (string.IsNullOrEmpty(fileInfo.error))
{
fileContents = fileInfo.text;
}
else
fileContents = fileInfo.error;
}
This is a coroutine, and to use it we need to call it using
yield return StartCoroutine(LoadDataFromResources(path + "/fileName.ext"));
Yet another remark: we can not write into this file or modify it as it's still inside the apk package.

Unity set default image sprite programmatically

I am trying to create inputfields in Unity 3D programmatically. I succeeded in this when running in the editor emulator, but trying to built into an android device just provides an error. "BCE0005: Unknown identifier: 'AssetDatabase'." Apparently this AssetDataBase is only available in editor.
inputFieldGO.AddComponent.<Image>();
var image : Image = inputFieldGO.GetComponent.<Image>();
image.sprite = AssetDatabase.GetBuiltinExtraResource.<Sprite>("UI/Skin/InputFieldBackground.psd");
image.type = Image.Type.Sliced;
How do I get around this? How do I set the sprite of this image to the default InputFieldBackground entirely programmatically, without using the AssetDataBase? I'd move the InputFieldBackground into the project resources, but I don't know where the file is or if it's even accessible.
AssetDatabase is an Editor class, that means that can be used in Editor but not in devices.
Unity Scripting Reference For AssetDatabase
Solution:
Do you have your files on Resources folder?
Try this:
Sprite newSprite = Resources.Load<Sprite>(spritePath);
From: Unity Scripting Reference for Resources.Load

Load an external text. Works on my computer, but not on app

I'm creating an app for Air 3.8 on Flash CS6. When I click on info_btn, the programm load an external textfile (here test.txt) and shows it in my dynamic text field (here info_txt).
It works when I CRTL+Enter, but not on my android device when I test the .apk file I created. I'm sure the reason is that the test.txt is not compiled in the apk, but I don't know how to change that. If you guys can help, it would be great.
Here is my code, if it can help:
var fl_TextLoader_3:URLLoader = new URLLoader();
var fl_TextURLRequest_3:URLRequest = new URLRequest("textes/test.txt");
fl_TextLoader_3.addEventListener(Event.COMPLETE, fl_CompleteHandler_3);
function fl_CompleteHandler_3(event:Event):void
{
var textData:String = new String(fl_TextLoader_3.data);
info_txt.text = textData;
}
info_btn.addEventListener(MouseEvent.CLICK, fl_infotxt);
function fl_infotxt(Event:MouseEvent):void {
fl_TextLoader_3.load(fl_TextURLRequest_3);
}
Usually mobile AIR projects will have an assets folder -- they do in Flash Builder and Flash Develop IDEs, so I assume CS6 has something similar.
The assets folder is where images and app icons go that will be delivered in the .apk. You can put any files and folders you want in there, including your text files. To package your text file into the .apk, you could create a assets/textes folder and put your test.txt in it.
Then the URL you would need in your code would be:
var fl_TextURLRequest_3:URLRequest = new URLRequest("assets/textes/test.txt");

Android Studio - Set custom fonts with asset manager in fragment

I'm rewriting a project of mine to incorporate fragments for use on tablets. I also decided to start using Android Studio with the conversion. Pretty straight forward but I've run into an issue on the pretty simple task of setting a custom font.
I started with these:
Custom fonts in android and
List of files in assets folder and its subfolders and this Set custom font for Android fragments
In Eclipse my class extended Activity and I did the following in the OnCreate with no problems.(Having a directory and file "assets/FUT_R.ttf")
TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
now trying to convert this Activity to an ActionBarActivity (fragment with V7 support library) and I've changed the above code to this. (where view is the inflated layout with one Framelayout for phones)
TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
and it crashes at run time with the lovely java.lang.RuntimeException: native typeface cannot be madeeven though i created and 'assets' directory on my new project with FUT_R.ttf in its root.
To confirm the asset manager I tried this code:
String[] f = null;
try {
f = getActivity().getAssets().list("");
}
catch (IOException e){
e.printStackTrace();
}
for(String f1:f){
Log.i("names",f1);
}
And got the following output:
07-26 07:40:40.134 2114-2114/com.myapp I/names: images
07-26 07:40:40.134 2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134 2114-2114/com.myapp I/names: webkit
I'm confused because no where in my 'assets' directory or project do I have these files and/or directories. Obviously the error is the system can't find my font file. WHAT AM I DOING WRONG? Any direction would be greatly appreciated, I've wasted far too much time on this stupid problem.
Thanks!
I finally stumbled upon this: Load a simple text file in Android Studio
which points out the assets folder in Android Studio has apparently been changed. Instead of its normal location in the root app folder (same level as libs, src, ect) it needs to be created under yourapp/src/main/assets. Hopefully this helps someone else. I wasted a lot of time on it!

Android Application to Install a font

I wanna create an application in android to install a Hindi Font. Can some one guide me how i can go about it ? is it possible ?
Add the font file you want to use in the assets folder of your Android application. Then you can load the font and use it like this:
AssetManager assetManager = getContext().getAssets();
Typeface tf = Typeface.createFromAsset(assetManager,"GILB.TTF");
TextView logo = (TextView)findViewById(R.id.logo);
logo.setTypeface(tf);
In this case, I've used the true type font Gill Sans Bold.
You cannot install a font on a device.
You can package a font as an asset with your application to be used by your application.

Categories

Resources