Search in sources :

Example 1 with WeightStyleSpan

use of com.taobao.luaview.extend.WeightStyleSpan in project LuaViewSDK by alibaba.

the class UDSpannableString method initSpannableStringBuilder.

private void initSpannableStringBuilder(LuaValue text, LuaValue config) {
    SpannableStringBuilder spannableStringBuilder = getSpannableStringBuilder();
    if (text != null && text.isstring()) {
        spannableStringBuilder = spannableStringBuilder.append(text.tojstring());
    }
    if (spannableStringBuilder.length() > 0) {
        if (config != null && config.istable()) {
            final int end = spannableStringBuilder.length();
            final int fontSize = DimenUtil.spToPx(config.get("fontSize").optint(-1));
            final Integer fontColor = ColorUtil.parse(LuaUtil.getValue(config, "fontColor"));
            final String fontName = config.get("fontName").optjstring(null);
            final LuaValue weightValue = config.get("fontWeight");
            final int fontWeight = LuaUtil.isNumber(weightValue) ? weightValue.optint(UDFontWeight.WEIGHT_NORMAL_INT) : UDFontWeight.getValue(weightValue.optjstring(UDFontWeight.WEIGHT_NORMAL));
            final LuaValue styleValue = config.get("fontStyle");
            final int fontStyle = LuaUtil.isNumber(styleValue) ? styleValue.optint(Typeface.NORMAL) : UDFontStyle.getValue(styleValue.optjstring(UDFontStyle.STYLE_NORMAL));
            final Integer backgroundColor = ColorUtil.parse(LuaUtil.getValue(config, "backgroundColor"));
            final boolean strikethrough = config.get("strikethrough").optboolean(false);
            final boolean underline = config.get("underline").optboolean(false);
            if (fontSize != -1) {
                // 字体大小
                spannableStringBuilder.setSpan(new AbsoluteSizeSpan(fontSize), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (fontColor != null) {
                // 字体颜色
                spannableStringBuilder.setSpan(new ForegroundColorSpan(fontColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (fontName != null && getLuaResourceFinder() != null) {
                // 字体
                spannableStringBuilder.setSpan(new CustomTypefaceSpan(fontName, getLuaResourceFinder().findTypeface(fontName)), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (fontWeight != -1 && fontWeight > UDFontWeight.WEIGHT_NORMAL_INT) {
                // 文字Weight
                spannableStringBuilder.setSpan(new WeightStyleSpan(fontWeight), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (fontStyle != -1 && (fontStyle >= Typeface.NORMAL && fontStyle <= Typeface.BOLD_ITALIC)) {
                // 文字样式
                spannableStringBuilder.setSpan(new StyleSpan(fontStyle), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (backgroundColor != null) {
                // 背景色
                spannableStringBuilder.setSpan(new BackgroundColorSpan(backgroundColor), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (strikethrough) {
                // 删除线
                spannableStringBuilder.setSpan(new StrikethroughSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (underline) {
                // 下划线
                spannableStringBuilder.setSpan(new UnderlineSpan(), 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) CustomTypefaceSpan(com.taobao.luaview.extend.CustomTypefaceSpan) WeightStyleSpan(com.taobao.luaview.extend.WeightStyleSpan) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) WeightStyleSpan(com.taobao.luaview.extend.WeightStyleSpan) LuaValue(org.luaj.vm2.LuaValue) SpannableStringBuilder(android.text.SpannableStringBuilder) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)1 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)1 BackgroundColorSpan (android.text.style.BackgroundColorSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 StrikethroughSpan (android.text.style.StrikethroughSpan)1 StyleSpan (android.text.style.StyleSpan)1 UnderlineSpan (android.text.style.UnderlineSpan)1 CustomTypefaceSpan (com.taobao.luaview.extend.CustomTypefaceSpan)1 WeightStyleSpan (com.taobao.luaview.extend.WeightStyleSpan)1 LuaValue (org.luaj.vm2.LuaValue)1