Android Html.fromHtml支持特殊字体(自定义任

2022-07-19  本文已影响0人  寻水的鱼Chock

预览效果

image.png

功能说明

使用方式

在工程根目录的build.gradle中添加

allprojects {
    repositories {
        maven { url 'https://www.jitpack.io' }
        ...
    }
}

添加依赖

implementation 'com.gitee.chockqiu:html-font-face:1.0'

使用示例

TextView tvHtml = findViewById(R.id.tvHtml);
TextView tvHtml2 = findViewById(R.id.tvHtml2);
TextView tvHtml3 = findViewById(R.id.tvHtml3);
HtmlTagRegister reg = new HtmlTagRegister();
HtmlFontFaceHandler fontFace = new HtmlFontFaceHandler("kfont") {
    @Override
    public Typeface onNeedFontFace(String faceName) {
        if (faceName.equalsIgnoreCase("DIN")) {
            return Typeface.createFromAsset(getAssets(),
                    "DIN_Alternate_Bold.ttf"
            );
        } else if (faceName.equalsIgnoreCase("YouSheBiaoTiHei")) {
            return Typeface.createFromAsset(getAssets(),
                    "YouSheBiaoTiHei.ttf"
            );
        }
        return null;
    }
};
reg.registerHtmlTag(fontFace);
tvHtml.setText(Html.fromHtml("<kfont color='#FF0000' size='16px'>愿有前程可奔赴,1234567890</kfont>", null, reg));
tvHtml2.setText(Html.fromHtml("<kfont color='#0000FF' face='DIN' size='16dp'>亦有岁月可回首,1234567890</kfont>", null, reg));
tvHtml3.setText(Html.fromHtml("<kfont color='#00FF00' face='YouSheBiaoTiHei' size='18sp'>且有情深共白头。1234567890</kfont>", null, reg));
如果本文对你有帮助就点个赞支持下吧~~~
上一篇下一篇

猜你喜欢

热点阅读