不要在 Android TextView 中以缩写句点换行文本 [英] Don't wrap text in Android TextView at period in abbreviation

查看:13
本文介绍了不要在 Android TextView 中以缩写句点换行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法控制 TextView 选择在哪里包装其文本?我遇到的问题是它想在句点处换行 - 所以像在美国和加拿大可用"这样的字符串可以在 U 和 S 之间的句点之后换行.有没有办法告诉 TextView 不要换行,除非有句点和空格,或者一些编程方式来控制换行?

Is there a way to control where a TextView chooses to wrap its text? The issue that I'm having is that it wants to wrap at periods - so a string like "Available in the U.S. and Canada" could wrap after the period between the U and the S. Is there a way to tell the TextView not to wrap unless there's a period and a space, or some programmatic way to control the wrapping?

推荐答案

我最终编写了自己的算法来仅在空白处打破文本.我最初使用的是 的 %20float[])" rel="noreferrer">breakText 方法Paint,但遇到了一些问题(实际上可能在此版本的代码中得到解决,但没关系).这不是我最好的代码块,它肯定可以清理一下,但它有效.因为我要覆盖 TextView,所以我只是从 onSizeChanged 方法确保有一个有效的宽度.

I ended up writing my own algorithm to break the text only on whitespace. I had originally used the breakText method of Paint, but was having some issues (that may actually be resolved in this version of the code, but that's OK). This isn't my best chunk of code, and it could definitely be cleaned up a bit, but it works. Since I'm overriding TextView, I just call this from the onSizeChanged method to ensure that there's a valid width.

private static void breakManually(TextView tv, Editable editable)
{
    int width = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight();
    if(width == 0)
    {
        // Can't break with a width of 0.
        return false;
    }

    Paint p = tv.getPaint();
    float[] widths = new float[editable.length()];
    p.getTextWidths(editable.toString(), widths);
    float curWidth = 0.0f;
    int lastWSPos = -1;
    int strPos = 0;
    final char newLine = '
';
    final String newLineStr = "
";
    boolean reset = false;
    int insertCount = 0;

    //Traverse the string from the start position, adding each character's
    //width to the total until:
    //* A whitespace character is found.  In this case, mark the whitespace
    //position.  If the width goes over the max, this is where the newline
    //will be inserted.
    //* A newline character is found.  This resets the curWidth counter.
    //* curWidth > width.  Replace the whitespace with a newline and reset 
    //the counter.

    while(strPos < editable.length())
    {
        curWidth += widths[strPos];

        char curChar = editable.charAt(strPos);

        if(((int) curChar) == ((int) newLine))
        {
            reset = true;
        }
        else if(Character.isWhitespace(curChar))
        {
            lastWSPos = strPos;
        }
        else if(curWidth > width && lastWSPos >= 0)
        {
            editable.replace(lastWSPos, lastWSPos + 1, newLineStr);
            insertCount++;
            strPos = lastWSPos;
            lastWSPos = -1;
            reset = true;
        }

        if(reset)
        {
            curWidth = 0.0f;
            reset = false;
        }

        strPos++;
    }

    if(insertCount != 0)
    {
         tv.setText(editable);
    }
}

这篇关于不要在 Android TextView 中以缩写句点换行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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