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

标签 android fonts textview

这是在预览版和设备上发生的情况: Text bug

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);
    }
}

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

<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>

如您所见,左侧的部分,如 'j' 和 'f' 被切断了。

设置内边距或边距无效。

当从其他程序中使用时,此字体适合它的框架。

提前致谢。

编辑: @play_err_ 提到的不是我的解决方案。

  • 我在最终版本中使用了一个自动调整大小的 TextView ,因此添加空格会非常困难。
  • 我需要解释一下为什么其他程序(例如 Photoshop、After Effects...)可以计算出正确的边界框而 android 不能
  • 我也在动态加载不同的字体,我不想创建一个

    if(badfont)
         addSpaces()
    

最佳答案

这个答案让我走上了正确的道路: 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());
        }
    }

关于Android textview文本在自定义字体的两侧被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44074858/

相关文章:

javascript - Webview:window.close 不起作用

Python ImageFont 和 ImageDraw 检查字符支持的字体

android - 强制 TextView 与多行文本一起使用

android - 如何在 google play 中显示我的 android 应用程序 apk 文件的原始大小?

java - 在 Cocos2dx 的主/UI 线程上运行代码

java - 如何将类作为参数传递给静态方法

java - 如何在 iText 5 中设置字体系列?

css - 将字体添加到 rails

android - 以编程方式居中 TextView 文本

java - Android 缩放文本以适合