Code example for TextPaint

Methods: ascentdescent

0
    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, so nothing to do. 
            result = specSize;
        } else { 
            // The lines should already be broken up. Calculate our max desired height 
            // for our current mode. 
            int numLines;
            if (mExpanded) {
                numLines = mBreakerExpanded.getLines().size();
            } else { 
                numLines = mBreakerCollapsed.getLines().size();
            } 
            result = numLines * (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom();
 
            // Respect AT_MOST value if that was what is called for by measureSpec. 
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            } 
        } 
        return result;
    } 
 
    /** 
     * Render the text 
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas) 
     */ 
    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        List<int[]> lines;
        LineBreaker breaker;
        if (mExpanded) {
            breaker = mBreakerExpanded;
            lines = mBreakerExpanded.getLines();
        } else { 
            breaker = mBreakerCollapsed;
            lines = mBreakerCollapsed.getLines();
        } 
 
        mTextPaint.setColor(mCurTextColor);
 
        float x = getPaddingLeft();
        float y = getPaddingTop() + (-mAscent);
        for (int i = 0; i < lines.size(); i++) {
            // Draw the current line. 
            int[] pair = lines.get(i);
            canvas.drawText(mText, pair[0], pair[1] + 1, x, y, mTextPaint);
 
            // Draw the ellipsis if necessary. 
            if (i == lines.size() - 1) {
                if (breaker.getRequiredEllipsis()) {
                    canvas.drawText(mStrEllipsis, x + breaker.getLengthLastEllipsizedLine(), y, mTextPaint);
                    if (mDrawEllipsizeMoreString) {
                        int lastColor = mTextPaint.getColor();
                        mTextPaint.setColor(mColorEllipsizeMore);
                        if (mRightAlignEllipsizeMoreString) {
                            // Seems to not be right... 
                            canvas.drawText(mStrEllipsisMore, canvas.getWidth()
                                    - (breaker.getLengthEllipsisMore() + getPaddingRight() + getPaddingLeft()), y,
                                    mTextPaint);
                        } else { 
                            canvas.drawText(mStrEllipsisMore, x + breaker.getLengthLastEllipsizedLinePlusEllipsis(), y,
                                    mTextPaint);
                        } 
                        mTextPaint.setColor(lastColor);
                    } 
                } 
            } 
 
            y += (-mAscent + mTextPaint.descent());
            if (y > canvas.getHeight()) {
                break; 
            } 
        } 
    } 
 
    public boolean getIsExpanded() { 
        return mExpanded;
    } 
 
    public void expand() { 
        mExpanded = true;
        requestLayout();
        invalidate();
    } 
 
    public void collapse() { 
        mExpanded = false;
        requestLayout();
        invalidate();
    } 
 
    private int breakWidth(int availableWidth) {
        int widthUsed = 0;
        if (mExpanded) {
            widthUsed = mBreakerExpanded.breakText(mText, availableWidth - getPaddingLeft() - getPaddingRight(),
                    mTextPaint);
        } else { 
            widthUsed = mBreakerCollapsed.breakText(mText, mStrEllipsis, mStrEllipsisMore, mMaxLines, availableWidth
                    - getPaddingLeft() - getPaddingRight(), mTextPaint);
        }