Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

android - Workaround for EditText ignoring lineSpacingMultiplier

Due to the bug mentioned in I can not adjust the edittext linespacing and https://issuetracker.google.com/issues/37009353#comment17

I tried the following workaround, by having custom onTextChanged

LinedEditText.java:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

/**
 * Created by yccheok on 24/3/2018.
 */

public class LinedEditText extends android.support.v7.widget.AppCompatEditText {
    public LinedEditText(Context context) {
        super(context);
        initPaint();
    }

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

    public LinedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x80000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int left = getLeft();
        int right = getRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int height = getHeight();
        int lineHeight = getLineHeight();
        int count = (height-paddingTop-paddingBottom) / lineHeight;

        float originalLineHeight = lineHeight / getLineSpacingMultiplier();

        for (int i = 0; i < count; i++) {
            float baseline = lineHeight * (i + 1) + paddingTop - mPaint.descent() - (lineHeight - originalLineHeight);
            canvas.drawLine(
                    left + paddingLeft,
                    baseline,
                    right - paddingRight,
                    baseline,
                    mPaint
            );
        }

        super.onDraw(canvas);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        if (lengthBefore != lengthAfter) {
            float add = getLineSpacingExtra();
            float mul = getLineSpacingMultiplier();
            setLineSpacing(0f, 1f);
            setLineSpacing(add, mul);
        }
    }

    private Paint mPaint = new Paint();
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <mypackage.LinedEditText
        android:id="@+id/edit_text"
        android:gravity="top"
        android:padding="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:background="@android:color/transparent"
        android:textSize="18sp"
        android:singleLine="false"
        android:lineSpacingMultiplier="2.5"
        android:lineSpacingExtra="0dp"
        android:inputType="textMultiLine" />

</FrameLayout>

However, it still have problem on the cursor. When you press ENTER to move the cursor to next line, the cursor is drawn by ignoring line spacing multiplier. When you start typing, only then the position of cursor will be corrected.

Please see screenshot

Before pressing ENTER

enter image description here

After pressing ENTER (Cursor is ignoring lineSpacingMultiplier)

enter image description here

Start typing (Cursor is considering lineSpacingMultiplier)

enter image description here

This is a pretty annoying bug, and Google is not yet fixing it.

I was wondering, does anyone of you come across a solid workaround? Do you mind to share with us?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Override onMeasure in edit and setBounds in cursor drawable.

Reference: https://github.com/hanks-zyh/LineHeightEditText/issues/1#issuecomment-503476003


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...