Code example for TextPaint

Methods: ascent, getTextBounds

0
    } 
 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
        mTextPaint.getTextBounds(mText, 0, mText.length(), text_bounds);
        setMeasuredDimension( 
                measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    } 
 
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
 
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text 
            result = text_bounds.height() + 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;
    } 
 
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
 
        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text