美文网首页
UI图与TextView之争

UI图与TextView之争

作者: OCTSJimmy | 来源:发表于2016-10-24 22:22 被阅读0次

本文起因:UI给的标注图上,两个垂直TextView之间,标注了xxdp。标注符号在上一行文字底部,紧贴文字,以及下一行文字顶部,也是紧贴文字。

一两行问题不大,可是某些情况下可能会有七八行甚至十来行,完,出事了,发现高度不够。

截图细查,发现是TextView设置的MarginTop按图上标的xxdp不行,因为MarginTop是按照TextView的外框来算的,文字到外框会有点点白边。

这个白边上下也就1dp左右,一两行不怎么看得出,十来行放一起,完,这就20dp,这就偏得有点远了。

下面,我们通过自定义一个TextView来解决这个问题:

首先几个概念需要先知道,大家知道的一起复习一下哈,不知道的随便看看留个印象。

储备知识:FontMetrics

其中存在:top,ascent,descent,bottom,leading这四个量。

首先:FontMetrics这个类的作用是用于文字测量。
接着:一个文字可以划分为这么几个部分:

FontMetrics图例1

其中:

Baseline:

文本绘制的起始点,可以视为0点,其余参数都是以它为准进行计算的。需要注意的是:向上为负数,向下为正数

Top:

文本的最高点,注意,这里包括了顶部的所有注音符号。什么是注音符号?汉语拼音您还记得不?对,那上面的符号就是注音符号。
注意,这个数值一般为负值。

Ascent:

从Baseline开始,至注音符号之前,也就是不含有注音符号的高度,一般称为上坡度。
注意,这个数值一般为负值。

Bottom:

文本的最低点,注意,这里也包括了底部的所有注音符号,有某些国家的注音符号是在底部标注的。
注意,这个数值一般为正值。

Descent:

从Baseline开始,到底部注音符号之前,与Ascent正好相反,也是不含有注音符号的高度,一般称为下坡度。
注意,这个数值一般为正值。

Leading:

表示上一行文本从Descent至本行文本Ascent的距离。一般称为行间距。

OK,补充了如上知识后,可以瞬间得出,我们输入的文本,几乎不带有注音符号,因此可以考虑移除Math.abs(top) - Math.abs(ascent)的距离,以及Math.abs(bottom) - Math.abs(descent)的距离。

好的,我们上源码:

package com.xxx.xxx.ui.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Paint.FontMetricsInt;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {
      public CustomTextView(Context context) {
            super(context);
      }

      public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
      }

      public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
      }
      @TargetApi(VERSION_CODES.LOLLIPOP)

      public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
      }

      private FontMetricsInt fontMetricsInt;

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int heightAndState = getMeasuredHeightAndState();
            int widthAndState = getMeasuredWidthAndState();
            int heightModeOrigin = MeasureSpec.getMode(heightMeasureSpec);
            int heightSizeOrigin = MeasureSpec.getSize(heightAndState);
            int heightMode = MeasureSpec.getMode(heightAndState);
            if (fontMetricsInt == null) {
                  fontMetricsInt = new FontMetricsInt();
                  getPaint().getFontMetricsInt(fontMetricsInt);
            }
            int top = Math.abs(fontMetricsInt.top);
            int ascent = Math.abs(fontMetricsInt.ascent);
            int bottom = Math.abs(fontMetricsInt.bottom);
            int descent = Math.abs(fontMetricsInt.descent);
            int heightSize = heightSizeOrigin - (top - ascent) - (bottom - descent);
            if (heightModeOrigin == MeasureSpec.AT_MOST) {
                  int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                  setMeasuredDimension(widthAndState, newHeightMeasureSpec);
            }
      }
}

源码很简单,但是,简书居然没有贴源码的功能,真不专业……果然还是CSDN靠谱么……

相关文章

网友评论

      本文标题:UI图与TextView之争

      本文链接:https://www.haomeiwen.com/subject/vspluttx.html