Code example for TextPaint

Methods: getDesiredWidth, getTypeface, setFakeBoldText, setTextSkewX, setTypeface

0
     * variants, so you may need to use 
     * {@link #setTypeface(Typeface, int)} to get the appearance 
     * that you actually want. 
     */ 
    public void setTypeface(Typeface tf) {
        if (mTextPaint.getTypeface() != tf) {
            mTextPaint.setTypeface(tf);
 
            measureContent(); 
        } 
    } 
 
    /** 
     * Sets the typeface and style in which the text should be displayed, 
     * and turns on the fake bold and italic bits in the Paint if the 
     * Typeface that you provided does not have all the bits in the 
     * style that you specified. 
     * 
     */ 
    public void setTypeface(Typeface tf, int style) {
        if (style > 0) {
            if (tf == null) {
                tf = Typeface.defaultFromStyle(style);
            } else { 
                tf = Typeface.create(tf, style);
            } 
 
            setTypeface(tf);
            // now compute what (if any) algorithmic styling is needed 
            int typefaceStyle = tf != null ? tf.getStyle() : 0;
            int need = style & ~typefaceStyle;
            mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
            mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
        } else { 
            mTextPaint.setFakeBoldText(false);
            mTextPaint.setTextSkewX(0);
            setTypeface(tf);
        } 
    } 
 
    /** 
     * Return the current typeface and style that the Paint 
     * using for display. 
     */ 
    public Typeface getTypeface() {
        return mTextPaint.getTypeface();
    } 
 
    /** 
     * Set a single text color for all states 
     * @param color Color value such as {@link Color#WHITE} or {@link Color#argb(int, int, int, int)} 
     */ 
    public void setTextColor(int color) {
        setTextColor(ColorStateList.valueOf(color));
    } 
 
    /** 
     * Set the text color as a state list 
     * @param colorStateList ColorStateList of text colors, such as inflated from an R.color resource 
     */ 
    public void setTextColor(ColorStateList colorStateList) {
        mTextColors = colorStateList;
        updateTextColors(getState());
    } 
 
    /** 
     * Optional Path object on which to draw the text.  If this is set, 
     * TextDrawable cannot properly measure the bounds this drawable will need. 
     * You must call {@link #setBounds(int, int, int, int) setBounds()} before 
     * applying this TextDrawable to any View. 
     * 
     * Calling this method with <code>null</code> will remove any Path currently attached. 
     */ 
    public void setTextPath(Path path) {
        if (mTextPath != path) {
            mTextPath = path;
            measureContent(); 
        } 
    } 
 
    /** 
     * Internal method to take measurements of the current contents and apply 
     * the correct bounds when possible. 
     */ 
    private void measureContent() { 
        //If drawing to a path, we cannot measure intrinsic bounds 
        //We must resly on setBounds being called externally 
        if (mTextPath != null) {
            //Clear any previous measurement 
            mTextLayout = null;
            mTextBounds.setEmpty();
        } else { 
            //Measure text bounds 
            float desired = Layout.getDesiredWidth(mText, mTextPaint);
            mTextLayout = new StaticLayout(mText, mTextPaint, (int)desired,
                    mTextAlignment, 1.0f, 0.0f, false);
            mTextBounds.set(0, 0, mTextLayout.getWidth(), mTextLayout.getHeight());
        }