Example usage for android.text.style MetricAffectingSpan MetricAffectingSpan

List of usage examples for android.text.style MetricAffectingSpan MetricAffectingSpan

Introduction

In this page you can find the example usage for android.text.style MetricAffectingSpan MetricAffectingSpan.

Prototype

MetricAffectingSpan

Source Link

Usage

From source file:com.gm.goldencity.util.Utils.java

/**
 * Apply typeface to a plane text and return spannableString
 *
 * @param text     Text that you want to apply typeface
 * @param typeface Typeface that you want to apply to your text
 * @return spannableString//  ww  w . j  a  v a  2s.c o m
 */
public static SpannableString applyTypefaceToString(String text, final Typeface typeface) {
    SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new MetricAffectingSpan() {
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(typeface);

            // Note: This flag is required for proper typeface rendering
            p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(typeface);

            // Note: This flag is required for proper typeface rendering
            tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
    }, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}