是否可以在类似ElegantButtonNumber的库中更改对象的字体?

时间:2018-08-16 20:08:31

标签: java android android-library android-styles

最近,我在一个项目中使用这个Elegant Button Number库。现在我想更改字体或强制其将数字更改为另一种格式[国有化]。我已经使用这种方法来更改TextView

FontHelper:

public class FontHelper {
private static FontHelper instance;
private static Typeface persianTypeface;

private FontHelper(Context context) {
    persianTypeface = Typeface.createFromAsset(context.getAssets(), "IRANSans.ttf");
}

public static synchronized FontHelper getInstance(Context context) {
    if (instance == null)
        instance = new FontHelper(context);
    return instance;
}

public Typeface getPersianTextTypeface() {
    return persianTypeface;
}
}

FormatHelper

public class FormatHelper {
private static String[] persianNumbers = new String[]{"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"};

public static String toPersianNumber(String text) {
    if (text.isEmpty())
        return "";
    String out = "";
    int length = text.length();
    for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        if ('0' <= c && c <= '9') {
            int number = Integer.parseInt(String.valueOf(c));
            out += persianNumbers[number];
        } else if (c == '٫') {
            out += '،';
        } else {
            out += c;
        }

    }
    return out;
}
}

PsTextView

public class PsTextView extends android.support.v7.widget.AppCompatTextView {
public PsTextView(Context context) {
    super(context);
    if (!isInEditMode())
        setTypeface(FontHelper.getInstance(context).getPersianTextTypeface());
}

public PsTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (!isInEditMode())
        setTypeface(FontHelper.getInstance(context).getPersianTextTypeface());
}

public PsTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode())
        setTypeface(FontHelper.getInstance(context).getPersianTextTypeface());
}

@Override
public void setText(CharSequence text, BufferType type) {
    if (text != null)
        text = FormatHelper.toPersianNumber(text.toString());
    super.setText(text, type);
}
}

因此,如您在上文中所见,我使用此方法将TextView替换为PsTextView,以更改数字和字母。因此可以更改此库的数字或编号或以某种方式对其进行自定义吗?无论如何,github页面得到了这个答案,并且没有响应。

0 个答案:

没有答案