Android: Use two Custom Fonts for one EditText

三世轮回 提交于 2020-01-07 06:25:08

问题


This may sound a bit fishy but, even i dont know if this is possible in android or not. The requirement is i need to write two languages in the edittext, one urdu and the other is default english, now the problem is in the typeface, since i have to use the urdu typeface as a default, i used setTypeface like the following:

editText.setTypeface(englishFont);

But the problem is the english in this typeface is not good, in fact it is smaller than usual for this i have to use another typeface for English, but this creates the problem, i get the getText from the editText, detect the urdu and english characters and then use spannableStringBuilder to change the typeface accordingly as following:

if(Arrays.asList(urduCodes).contains(Integer.toHexString(text.charAt(i)).toUpperCase()))
                {
                    System.out.println("different: "+text.charAt(i));
                    SpannableStringBuilder urduBLD;
                    urduBLD = new SpannableStringBuilder("");
                    urduBLD.append(edi.getText().charAt(i));
                    System.out.println("urduBLD: "+urduBLD);
                    urduBLD.setSpan(new StyleSpan(urduFont.getStyle()),urduBLD.length(),urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(urduBLD);



                } else 
                {
                    System.out.println(text.charAt(i)+"-Code:"+(int)text.charAt(i)+"-"+Integer.toHexString(text.charAt(i)));
                    SpannableStringBuilder engBLD;
                    engBLD = new SpannableStringBuilder("");
                    engBLD.append(edi.getText().charAt(i));
                    System.out.println("engBLD: "+engBLD);
                    engBLD.setSpan(new StyleSpan(englishFont.getStyle()),engBLD.length(),engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(engBLD);


                }

But this does not solve the problem, the text in editText remains the same, no change in typeface. Is this possible or not??


回答1:


After doing some research and hardwork finally i was able to create a universal function for this:

public SpannableStringBuilder setWord(String text)
    {
        tokenList = new ArrayList<String>();

        startIndext = 0;
        endIndex = 0;
        isEnglish = false;

        stringBuilder = new SpannableStringBuilder("");

        Log.i("CustomUrduKeyBoard","inside setWord text is: "+text+" stringBuilder: "+stringBuilder);
        if(text == null)
        {

        } else 
        {

            for (int i = 0; i < text.length(); i++) 
            {
//              System.out.println(text.charAt(i) + "-Code:" + (int) text.charAt(i)
//                      + "-" + Integer.toHexString(text.charAt(i)));
                if (text.charAt(i) < 255) 
                {
                    if (!isEnglish) 
                    {
                        endIndex = i;
                        String token = text.substring(startIndext, endIndex);

                        SpannableStringBuilder urduBLD;
                        urduBLD = new SpannableStringBuilder("");
                        urduBLD.append(token);

                        urduBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        stringBuilder.append(urduBLD);
                        tokenList.add(urduBLD.toString());
                        startIndext = endIndex;
                    }
                    isEnglish = true;
                    // english
                    continue;
                } else 
                {
                    if (isEnglish) 
                    {
                        endIndex = i;
                        String token = text.substring(startIndext, endIndex);

                        SpannableStringBuilder engBLD;
                        engBLD = new SpannableStringBuilder("");
                        engBLD.append(token);

                        engBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                        stringBuilder.append(engBLD);
                        tokenList.add(engBLD.toString());
                        startIndext = endIndex;
                    }
                    isEnglish = false;
                    // urdu
                    continue;
                }
            }

            String token = text.substring(startIndext, text.length());

            for (int i = 0; i < token.length(); i++) 
            {
                if (token.charAt(i) < 255) 
                {

                    SpannableStringBuilder urduBLD;
                    urduBLD = new SpannableStringBuilder("");
                    urduBLD.append(token);

                    urduBLD.setSpan(new CustomTypefaceSpan("", englishFont),0,urduBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(urduBLD);
                    tokenList.add(urduBLD.toString());

                    break;
                } else
                {

                    SpannableStringBuilder engBLD;
                    engBLD = new SpannableStringBuilder("");
                    engBLD.append(token);

                    engBLD.setSpan(new CustomTypefaceSpan("", urduFont),0,engBLD.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    stringBuilder.append(engBLD);
                    tokenList.add(engBLD.toString());

                    break;
                }
            }

        }

        return stringBuilder;
    }


来源:https://stackoverflow.com/questions/36542631/android-use-two-custom-fonts-for-one-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!