简体   繁体   中英

“word-wrap: break-word” in EditText

Has an android a css-like property "word-wrap"?

I just want to my text is not wrapped by spaces, dashes, etc., something like this:

  1. hello, w
  2. orld

Instead of

  1. hello,
  2. world

Unfortunately, android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.

class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;

    private WordBreakTransformationMethod() {}

    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }

        return instance;
    }

    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};

    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};

    @Override
    protected char[] getOriginal()
    {
        return original;
    }

    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}

'\‑' is non-breaking dash, '\ ' is non-breaking space. Unfortunately, UTF hasn't non-breaking analog for slash ('/'), but you can use division slash (' ∕ ').

For use this code, set instance of WordBreakTransformationMethod to your EditText.

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM