竖排文本控件

来源:互联网 发布:被冒名网络贷款 编辑:程序博客网 时间:2024/05/16 01:25

源码:https://github.com/lybeat/PlumbTextView

PlumbTextView

PlumbTextView是一个竖排列的文本控件。你可以很容易使用它定义多种竖排文本风格。

Feature

  1. 将文本竖直排列;

  2. 可以设置文本的列距和字距;

  3. 可以添加一个正则表达式去分割文本。PlumbTextView会在正则表达式所包含的字符处换列,并且其中的字符不会显示在PlumbTextView中;

  4. 可以在每一列文本的坐标添加一跟竖线;

  5. 可以为文本设置字体风格和第三方字体。

Screenshot

plumb_textview.gif

Gradle Dependency

  1. compile 'cc.sayaki.widget:plumb-textview:1.0.1'

Usage

你可以很容易的使用PlumbTextView,就像使用TextView那样。只需要在xml或者Java代码中设置你想要的属性效果就行了。

  1. <cc.sayaki.widget.PlumbTextView
  2.    android:layout_width="wrap_content"
  3.    android:layout_height="wrap_content"
  4.    android:paddingBottom="30dp"
  5.    android:paddingTop="30dp"
  6.    sayaki:columnSpacing="20dp"
  7.    sayaki:leftLine="true"
  8.    sayaki:leftLineColor="@color/colorAccent"
  9.    sayaki:leftLinePadding="4dp"
  10.    sayaki:letterSpacing="6dp"
  11.    sayaki:regex="[,。?!]"
  12.    sayaki:text="@string/text"
  13.    sayaki:textColor="@color/colorAccent"
  14.    sayaki:textSize="16sp"
  15.    sayaki:textStyle="bold|italic" />

Thinking

接下来讲讲PlumbTextView的实现思路。
思路很简单,主要就是在绘制完一个字符之后,将下次绘制的位置垂直向下移动一个字符的高度(包括字符本身的高度和两个字符之间的字距)。只是垂直绘制文字很简单,不过处理换列是一个比较麻烦的地方。

PlumbTextView有两种换列情况:

  1. 单列文本超过了PlumbTextView可以显示的内容高度;

  2. 提供了正则表达式,PlumbTextView会在正则表达式中所包含的字符处换列。

首先测量PlumbTextView自身的高度

  1. if (heightMode == MeasureSpec.EXACTLY) {
  2.     height = heightSize;
  3. } else {
  4.     if (!TextUtils.isEmpty(regex)) {
  5.         height = 0;
  6.         String[] texts = text.toString().split(regex);
  7.         for (String s : texts) {
  8.             height = Math.max(height, getDesiredHeight(s));
  9.         }
  10.         height += letterSpacing;
  11.     } else {
  12.         height = getDesiredHeight(text.toString());
  13.     }
  14.     if (height > heightSize) {
  15.         height = heightSize;
  16.     }
  17. }

根据换列规则拆分文本

  1. if (!TextUtils.isEmpty(regex)) {
  2.     String[] texts = text.toString().split(regex);
  3.     for (String s : texts) {
  4.         getFormatTexts(s);
  5.     }
  6. } else {
  7.     getFormatTexts(text.toString());
  8. }
  9.  
  10. // 获取拆分后的文本
  11. private void getFormatTexts(String s) {
  12.     int contentHeight = height - getPaddingTop() - getPaddingBottom();
  13.     if (getDesiredHeight(s) > contentHeight) {
  14.         int count = contentHeight / charHeight;
  15.         int i = 0;
  16.         // 有的文本拆分过后可能仍然大于控件可显示的高度,需要再拆分
  17.         for (; i < getDesiredHeight(s) / contentHeight; i++) {
  18.             formatTexts.add(s.substring(* count, (+ 1) * count));
  19.         }
  20.         // 最后一列文本不满一列
  21.         if (getDesiredHeight(s) % contentHeight != 0) {
  22.             formatTexts.add(s.substring(* count, s.length()));
  23.         }
  24.     } else {
  25.         formatTexts.add(s);
  26.     }
  27. }

测量PlumbTextView的宽度

  1. if (widthMode == MeasureSpec.EXACTLY) {
  2.     width = widthSize;
  3. } else {
  4.     if (!TextUtils.isEmpty(regex)) {
  5.         width = columnWidth * formatTexts.size();
  6.     } else {
  7.         width = columnWidth * (getDesiredHeight(text.toString())
  8.                 / (height - getPaddingTop() - getPaddingBottom()) + 1);
  9.     }
  10.     if (width > widthSize) {
  11.         width = widthSize;
  12.     }
  13. }

上述操作均在onMeasure方法中,因此为了避免多次测量造成拆分后的文本重复,在每次拆分之前先清空formatTexts。

现在已经获得了按列拆分好的文本了,要绘制就变得简单了。

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3.     float x = width - getPaddingLeft() - getPaddingRight();
  4.     float y = getPaddingTop();
  5.     for (int i = 0; i < formatTexts.size(); i++) {
  6.         // 换列
  7.         x = i == 0 ? width - columnWidth + columnSpacing : x - columnWidth;
  8.         // 绘制每一列文本
  9.         for (int j = 0; j < formatTexts.get(i).length(); j++) {
  10.             // 向下移动绘制点
  11.             y = j == 0 ? charHeight - letterSpacing + getPaddingTop() : y + charHeight;
  12.             canvas.drawText(formatTexts.get(i), j, j + 1, x, y, textPaint);
  13.         }
  14.         if (leftLine) {
  15.             // 在每列文本之后绘制竖线
  16.             canvas.drawLine(- leftLinePadding, getPaddingTop(),
  17.                     x - leftLinePadding, y + letterSpacing, leftLinePaint);
  18.         }
  19.     }
  20. }
原创粉丝点击