/** ~~~~~
* Created with the AppyBuilder Code Editor.
* This is a template for basic Extension.
* Modify this template to customize your extension.
*
* **** NOTE: DO NOT use a package name.
* **** The package name will be created for you automatically.
* **** Adding a package name will cause a compile error
*/
import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.*;
import com.google.appinventor.components.common.*;
import android.view.View;
import android.widget.TextView;
import android.graphics.Typeface;
import java.io.File;
@DesignerComponent(version = 1, description = "This Extension was created with the AppyBuilder Code Editor.
" +
"Create your own here:
https://editor.appybuilder.com
",
category = ComponentCategory.EXTENSION,
nonVisible = true, iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE")
public class YourFont extends AndroidNonvisibleComponent {
private ComponentContainer container;
private Context context;
private Typeface fontTypeface;
private boolean isRepl = false;
/**
* @param container container, component will be placed in
*/
public YourFont(ComponentContainer container) {
super(container.$form());
this.container = container;
context = (Context) container.$context();
//// Provided by Taifun
if (form instanceof ReplForm) { // Note: form is defined in our superclass
isRepl = true;
}
////
}
@SimpleFunction(description="Sets Component's Font to CustomFont")
public void SetFont(AndroidViewComponent component){
TextView tv = (TextView)component.getView();
int style = tv.getTypeface().getStyle();
tv.setTypeface(Typeface.create(fontTypeface, style));
}
@SimpleFunction(description="Resets Component's Font to default Sans Serif Font")
public void ResetFont(AndroidViewComponent component){
TextView tv = (TextView)component.getView();
int style = tv.getTypeface().getStyle();
tv.setTypeface(Typeface.create(Typeface.SANS_SERIF, style));
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_ASSET, defaultValue = "")
@SimpleProperty(userVisible=true)
public void CustomFont(String fontFileName)
{
if (fontFileName == null || fontFileName.isEmpty() || fontFileName == "") {
return;
}
Typeface localTypeface1 = null;
if (!fontFileName.contains("/")) {
if (isRepl) {
File file = new File("/storage/emulated/0/AppInventor/assets/" + fontFileName);
localTypeface1 = Typeface.createFromFile(file);
} else {
localTypeface1 = Typeface.createFromAsset(context.getAssets(), fontFileName);
}
} else {
File file = new File(fontFileName);
localTypeface1 = Typeface.createFromFile(file);
}
if (localTypeface1 == null) return;
this.fontTypeface = localTypeface1;
}
}