Android textview文本在自定义字体的两侧被切断

and*_*ras 17 fonts android textview

这是在预览和设备上发生的事情: 文字错误

TextView没什么特别的,只是加载自定义字体:

public class TestTextView extends AppCompatTextView {

    public TestTextView(Context context) {
        super(context);

        init(context);
    }

    public TestTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public TestTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    void init(Context context) {

        Typeface t = Typeface.createFromAsset(context.getAssets(), "fonts/daisy.ttf");

        setTypeface(t);
    }
}
Run Code Online (Sandbox Code Playgroud)

布局也很基础,但以防万一:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/material_red200"
    android:orientation="vertical">    

    <*custompackage* .TestTextView
        android:gravity="left"
        android:padding="0dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="just some text for testing"
        android:textColor="@color/material_black"
        android:textSize="100dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,左侧部分,如"j"和"f"被切断.

设置填充或边距不起作用.

从其他程序使用时,此字体适合其框架.

提前致谢.

编辑:在我的案例中,@ play_err_提到的不是解决方案.

  • 我在最终版本中使用自动调整大小的textview,因此添加空格将非常困难.
  • 我需要解释为什么其他程序(例如photoshop,后效...)可以计算出一个合适的边界框而android不能
  • 我也动态加载不同的字体,我不想创建一个

    if(badfont)
         addSpaces()
    
    Run Code Online (Sandbox Code Playgroud)

and*_*ras 11

这个答案让我走上了正确的道路:https: //stackoverflow.com/a/28625166/4420543

因此,解决方案是创建自定义Textview并覆盖onDraw方法:

    @Override
    protected void onDraw(Canvas canvas) {
        final Paint paint = getPaint();
        final int color = paint.getColor();
        // Draw what you have to in transparent
        // This has to be drawn, otherwise getting values from layout throws exceptions
        setTextColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        // setTextColor invalidates the view and causes an endless cycle
        paint.setColor(color);

        System.out.println("Drawing text info:");

        Layout layout = getLayout();
        String text = getText().toString();

        for (int i = 0; i < layout.getLineCount(); i++) {
            final int start = layout.getLineStart(i);
            final int end = layout.getLineEnd(i);

            String line = text.substring(start, end);

            System.out.println("Line:\t" + line);

            final float left = layout.getLineLeft(i);
            final int baseLine = layout.getLineBaseline(i);

            canvas.drawText(line,
                    left + getTotalPaddingLeft(),
                    // The text will not be clipped anymore
                    // You can add a padding here too, faster than string string concatenation
                    baseLine + getTotalPaddingTop(),
                    getPaint());
        }
    }
Run Code Online (Sandbox Code Playgroud)


Dmi*_*tov 6

我在EditText.

我的第一次尝试是使用填充。视图大小增加,但文本仍被裁剪。

在此处输入图片说明

然后我查看了源代码TextView。在方法中调用onDraw方法Canvas.clipRect来执行此裁剪。

在此处输入图片说明

我在使用填充时绕过裁剪的解决方案:

1) ?reate 自定义类继承Canvas并覆盖方法clipRect

public class NonClippableCanvas extends Canvas {

    public NonClippableCanvas(@NonNull Bitmap bitmap) {
        super(bitmap);
    }

    @Override
    public boolean clipRect(float left, float top, float right, float bottom) {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

2) 创建自定义TextView和覆盖方法onSizeChangedonDraw.

在方法中onSizeChanged创建位图和画布。

在方法中onDraw通过将我们的自定义传递Canvas给方法来绘制位图super.onDraw。接下来,在目标画布上绘制此位图。

public class CustomTextView extends AppCompatTextView {
    private Bitmap _bitmap;
    private NonClippableCanvas _canvas;

    @Override
    protected void onSizeChanged(final int width, final int height,
                             final int oldwidth, final int oldheight) {
        if (width != oldwidth || height != oldheight) {
            _bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            _canvas = new NonClippableCanvas(_bitmap);
        }

        super.onSizeChanged(width, height, oldwidth, oldheight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        _canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        super.onDraw(_canvas);

        canvas.drawBitmap(_bitmap, 0, 0, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Yos*_*sef 6

我遇到了同样的问题,我为使用TextView.shadowLayer.

这是基于源代码[梅德Kopytov]带到这里

editTextOrTextView.setShadowLayer(editTextOrTextView.textSize, 0f, 0f, Color.TRANSPARENT)
Run Code Online (Sandbox Code Playgroud)

就是这样,现在canvas.clipRectin TextView.onDraw()不会切断卷曲字体的两侧。


Kik*_*iwa 5

重新设计的@Dmitry Kopytov 解决方案:

  • 在科特林
  • 回收旧位图
  • 添加文档
  • 如果无法创建位图(内存不足),则回退到默认的 TextView 渲染

代码:

/**
 * This TextView is able to draw text on the padding area.
 * It's mainly used to support italic texts in custom fonts that can go out of bounds.
 * In this case, you've to set an horizontal padding (or just end padding).
 *
 * This implementation is doing a render-to-texture procedure, as such it consumes more RAM than a standard TextView,
 * it uses an additional bitmap of the size of the view.
 */
class TextViewNoClipping(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
    private class NonClippableCanvas(@NonNull val bitmap: Bitmap) : Canvas(bitmap) {
        override fun clipRect(left: Float, top: Float, right: Float, bottom: Float): Boolean {
            return true
        }
    }

    private var rttCanvas: NonClippableCanvas? = null

    override fun onSizeChanged(width: Int, height: Int,
                               oldwidth: Int, oldheight: Int) {
        if ((width != oldwidth || height != oldheight) && width > 0 && height > 0) {
            rttCanvas?.bitmap?.recycle()
            try {
                Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)?.let {
                    rttCanvas = NonClippableCanvas(it)
                }
            } catch (t: Throwable) {
                // If for some reasons the bitmap cannot be created, we fall back on default rendering (potentially cropping the text).
                rttCanvas?.bitmap?.recycle()
                rttCanvas = null
            }
        }

        super.onSizeChanged(width, height, oldwidth, oldheight)
    }

    override fun onDraw(canvas: Canvas) {
        rttCanvas?.let {
            // Clear the RTT canvas from the previous font.
            it.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

            // Draw on the RTT canvas (-> bitmap) that will use clipping on the NonClippableCanvas, resulting in no-clipping
            super.onDraw(it)

            // Finally draw the bitmap that contains the rendered text (no clipping used here, will display on top of padding)
            canvas.drawBitmap(it.bitmap, 0f, 0f, null)

        } ?: super.onDraw(canvas) // If rtt is not available, use default rendering process
    }
}
Run Code Online (Sandbox Code Playgroud)