Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw Text with Specific Text Appearance

Tags:

android

paint

How to draw text with a specific TextAppearance?

Here is my code.

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    paint.setFakeBoldText(false);
    paint.setTextSize(textSize);
    // TODO: set the style of the text to a specific TextAppearance. E.g. @android:style/TextAppearance.DeviceDefault.Large.Inverse
    canvas.drawText(context.getResources().getString(R.string.myString), textOffsetX, textOffsetY, paint);

I have looked at TypeFace object, but I am not sure how to create a TypeFace from a TextAppearance.

like image 312
louis.luo Avatar asked Oct 30 '14 19:10

louis.luo


1 Answers

A coworker gave me a solution that uses a TextView as the a bridge for the TextAppearance

TextView textView = new TextView(context);
textView.setTextAppearance(context, android.R.style.TextAppearance.DeviceDefault.Large);
paint.setColor(textView.getCurrentTextColor());
paint.setTextSize(textView.getTextSize());
paint.setTypeface(textView.getTypeface());
like image 132
louis.luo Avatar answered Oct 11 '22 12:10

louis.luo