Android textview文本在自定义字体的两侧被切断 [英] Android textview text get cut off on the sides with custom font

查看:292
本文介绍了Android textview文本在自定义字体的两侧被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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

  public class TestTextView extends AppCompatTextView {
$ b $ public TestTextView(Context context){
super(上下文);

init(context);


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

init(context);

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

init(context);


void init(Context context){

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

setTypeface(t);




$ b $布局也是非常基本的,但以防万一:

 < 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>
$ b android:gravity =left
android:padding =0dp
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =只是一些测试文本
android:textColor =@ color / material_black
android:textSize =100dp/ >

< / LinearLayout>

正如您所看到的那样,左边的部分,如'j'和'f'被截断。



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

当使用其他程序时,该字体适合其框架。



在此先感谢。

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


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

      if(badfont)
    addSpaces()
    <


    $ b $ div class =h2_lin>解决方案

这个答案导致我到正确的路径:
https://stackoverflow.com/a/28625166/4420543



因此,解决方案是创建一个自定义Textview并重写onDraw方法:

pre $ code > @Override
protected void onDraw(Canvas canvas){
final Paint paint = getPaint();
final int color = paint.getColor();
//在透明
中绘制你必须要绘制的东西,否则从布局获取值会抛出异常
setTextColor(Color.TRANSPARENT);
super.onDraw(canvas);
// setTextColor使视图无效并导致无限循环
paint.setColor(color);

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

布局布局= 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(),
//文本不会被裁剪
//你也可以在这里添加一个填充,比字符串串联更快
baseLine + getTotalPaddingTop(),
getPaint());
}
}


This is what happens in the preview and on device:

TextView is nothing special, it just loads the custom font:

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

Layout is also very basic, but just in case:

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

As you can see, the left parts, like 'j' and 'f' are cut off.

Setting the padding or margin did not work.

This font fits into it's frame when using from other programs.

Thanks in advance.

Edit: What @play_err_ mentioned is not a solution in my case.

  • I am using in the final version a textview that resizes automatically, so adding spaces would be terribly difficult.
  • I need an explanation why other programs (eg photoshop, after effects...) can calculate a proper bounding box and android cannot
  • I am also loading different fonts dynamically and I do not want to create an

    if(badfont)
         addSpaces()
    

解决方案

This answer has led me to the right path: https://stackoverflow.com/a/28625166/4420543

So, the solution is to create a custom Textview and override the onDraw method:

    @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文本在自定义字体的两侧被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆