简体   繁体   中英

Android Studio Shared Preferences Set Font Style

Currently I'm trying to get the input choice from List Preference which has (Italic, Bold, Underlined) styles in it, but I am not quite sure how to accomplished this specifically.

In the past, I've successfully done it for font type, size and colors.

Font Type Example:

         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String s = sharedPreferences.getString("font_list", "gnuolane rg.ttf");
    Typeface face = Typeface.createFromAsset(getAssets(), "fonts/" + s);
    editText.setTypeface(face);

Font Size Example:

         String s2 = sharedPreferences.getString("font_size", "8");
    editText.setTextSize(Float.parseFloat(s2))

How can I achieve the same idea but with font styles, such as Bold, Italic, Underlined?

I think you can do this way

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int font_face = sharedPreferences.getInt("font_face", Typeface.NORMAL);
editText.setTypeface(Integer.toString(font_face));

Basically Typeface.BOLD , Typeface.BOLD_ITALIC , Typeface.ITALIC , Typeface.NORMAL is int as this link said.

Try this.

Store Font Style

private void storeFontStyle(int style){

        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor=sharedPreferences.edit();

        editor.putInt("font_face", style);

        editor.apply();

    }

Get font Style

private int getFontStyle(){

    SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int font_face = sharedPreferences.getInt("font_face", Typeface.NORMAL);

    return font_face;
}

Access Font Style

int font_face=getFontStyle();

    editText.setTypeface(null, font_face);

I can only answer the question for BOLD and ITALIC (I don't think it is possible to specifically underline EditText , but I might be wrong):

String fontSize = sharedPreferences.getString("font_size", "8");
String style= sharedPreferences.getInt("font_style", TypeFace.NORMAL);
String font = sharedPreferences.getString("font_list", "gnuolane rg.ttf");
Typeface face = Typeface.create(Typeface.createFromAsset(getAssets(), "fonts/" + s),style);
editText.setTextSize(Float.parseFloat(fontSize ))

As you can see, the style is just an integer variable inside Typeface that can be read via editText.getTypeface().getStyle() (and then saved to the sharedPreferences ).

Possible values for the style are:

Typeface.NORMAL
Typeface.BOLD
Typeface.ITALIC
Typeface.BOLD_ITALIC

But in order for this to work you have to make sure that the asset you are creating your font from supports the different font styles (which is normally not the case to my knowledge).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM