feat:android TextNode font support load from res
This commit is contained in:
parent
3a9b6c2a85
commit
26d0a38357
BIN
doric-android/app/src/main/res/font/font_iconfont.ttf
Normal file
BIN
doric-android/app/src/main/res/font/font_iconfont.ttf
Normal file
Binary file not shown.
@ -23,6 +23,8 @@ import android.util.TypedValue;
|
|||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.core.content.res.ResourcesCompat;
|
||||||
|
|
||||||
import com.github.pengfeizhou.jscore.JSObject;
|
import com.github.pengfeizhou.jscore.JSObject;
|
||||||
import com.github.pengfeizhou.jscore.JSValue;
|
import com.github.pengfeizhou.jscore.JSValue;
|
||||||
|
|
||||||
@ -30,6 +32,7 @@ import pub.doric.DoricContext;
|
|||||||
import pub.doric.extension.bridge.DoricPlugin;
|
import pub.doric.extension.bridge.DoricPlugin;
|
||||||
import pub.doric.shader.richtext.CustomTagHandler;
|
import pub.doric.shader.richtext.CustomTagHandler;
|
||||||
import pub.doric.shader.richtext.HtmlParser;
|
import pub.doric.shader.richtext.HtmlParser;
|
||||||
|
import pub.doric.utils.DoricLog;
|
||||||
import pub.doric.utils.DoricUtils;
|
import pub.doric.utils.DoricUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -122,12 +125,42 @@ public class TextNode extends ViewNode<TextView> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String font = prop.asString().toString();
|
String font = prop.asString().toString();
|
||||||
if (font.endsWith(".ttf")) {
|
String fontPath="";
|
||||||
Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), font);
|
String fontName=font;
|
||||||
|
if(font.contains("/")){
|
||||||
|
int separatorIndex=font.lastIndexOf("/");
|
||||||
|
fontPath=font.substring(0,separatorIndex+1);
|
||||||
|
fontName=font.substring(separatorIndex+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fontName.endsWith(".ttf")) {
|
||||||
|
fontName = fontName.replace(".ttf","");
|
||||||
|
}
|
||||||
|
|
||||||
|
int resId = getContext().getResources().getIdentifier(
|
||||||
|
fontName.toLowerCase(),
|
||||||
|
"font",
|
||||||
|
getContext().getPackageName());
|
||||||
|
if (resId > 0) {
|
||||||
|
try {
|
||||||
|
Typeface iconFont = ResourcesCompat.getFont(getContext(), resId);
|
||||||
view.setTypeface(iconFont);
|
view.setTypeface(iconFont);
|
||||||
|
}catch (Exception e){
|
||||||
|
DoricLog.e("Error Font asset "+font+" in res/font");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), font + ".ttf");
|
fontName = fontPath+
|
||||||
|
fontName +
|
||||||
|
".ttf";
|
||||||
|
try {
|
||||||
|
Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), fontName);
|
||||||
view.setTypeface(iconFont);
|
view.setTypeface(iconFont);
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
DoricLog.e(font+" not found in Assets");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
BIN
doric-demo/bundle/fonts/assets_iconfont.ttf
Normal file
BIN
doric-demo/bundle/fonts/assets_iconfont.ttf
Normal file
Binary file not shown.
@ -46,14 +46,35 @@ class TextDemo extends Panel {
|
|||||||
fontStyle: "bold_italic"
|
fontStyle: "bold_italic"
|
||||||
}),
|
}),
|
||||||
text({
|
text({
|
||||||
text: "This is Icon Font text \ue631",
|
text: "Icon Font text from \ue631 ",
|
||||||
|
textSize: 10,
|
||||||
font: 'iconfont'
|
font: 'iconfont'
|
||||||
}),
|
}),
|
||||||
text({
|
text({
|
||||||
text: "This is Icon Font text \ue631",
|
text: "Icon Font text from \ue631 ",
|
||||||
textSize: 30,
|
textSize: 30,
|
||||||
font: 'iconfont'
|
font: 'iconfont'
|
||||||
}),
|
}),
|
||||||
|
text({
|
||||||
|
text: "Icon Font text from res/font/ \ue631 ",
|
||||||
|
textSize: 10,
|
||||||
|
font: 'font_iconfont'
|
||||||
|
}),
|
||||||
|
text({
|
||||||
|
text: "Icon Font text from res/font/ \ue631 ",
|
||||||
|
textSize: 30,
|
||||||
|
font: 'font_iconfont.ttf'
|
||||||
|
}),
|
||||||
|
text({
|
||||||
|
text: "Icon Font text from assets/fonts/ \ue631 ",
|
||||||
|
textSize: 10,
|
||||||
|
font: 'fonts/assets_iconfont'
|
||||||
|
}),
|
||||||
|
text({
|
||||||
|
text: "Icon Font text from assets/fonts/ \ue631 ",
|
||||||
|
textSize: 30,
|
||||||
|
font: 'fonts/assets_iconfont.ttf'
|
||||||
|
}),
|
||||||
text({
|
text({
|
||||||
text: "This is line Spaceing 0,\nSecond line",
|
text: "This is line Spaceing 0,\nSecond line",
|
||||||
maxLines: 0,
|
maxLines: 0,
|
||||||
|
Reference in New Issue
Block a user