当前位置 : 主页 > 编程语言 > c++ >

根据实际文本宽度自动缩放文字大小

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt private float adjustTvTextSize(TextView tv, int maxWidth, String text) { int avaiWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight() - 10; if (avaiWidth = 0 || StringUtils.isEmpty(text)) { return tv.getPaint().getTex
gistfile1.txt
private float adjustTvTextSize(TextView tv, int maxWidth, String text) {
        int avaiWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight() - 10;

        if (avaiWidth <= 0 || StringUtils.isEmpty(text)) {
            return tv.getPaint().getTextSize();
        }

        TextPaint textPaintClone = new TextPaint(tv.getPaint());
        // note that Paint text size works in px not sp
        float trySize = textPaintClone.getTextSize();

        while (textPaintClone.measureText(text) > avaiWidth) {
            trySize--;
            textPaintClone.setTextSize(trySize);
        }

        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        return trySize;
    }
网友评论