Code example for TextPaint

Methods: ascent, descent, measureText

0
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text 
            result = (int) mTxPaint.measureText(getText().toString()) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec 
                result = Math.min(result, specSize);
            } 
        } 
        return result;
    } 
 
    /** 
     * Determines the height of this view 
     * @param measureSpec A measureSpec packed into an int 
     * @return The height of the view, honoring constraints from measureSpec 
     */ 
    final int fix = 2; // Maybe font not quite right?
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
 
        int mAscent = (int) mTxPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text (beware: ascent is a negative number) 
            result = (int) (-mAscent + mTxPaint.descent()) + getPaddingTop()
                    + getPaddingBottom() + fix;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec 
            	if (result > specSize){
            		mOverSize = result - specSize;