Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set Text Alignment Justified for the text ? #30

Open
icstar opened this issue Feb 5, 2019 · 1 comment
Open

How to set Text Alignment Justified for the text ? #30

icstar opened this issue Feb 5, 2019 · 1 comment

Comments

@icstar
Copy link

icstar commented Feb 5, 2019

I am happy to work with this library. For now, I want to add justify alignment on the text. The text justified property will need to be kept for both status Expanded and Collapsed. how can I do it?

@aci9evil
Copy link

private static void justify(final TextView textView) {

    final AtomicBoolean isJustify = new AtomicBoolean(false);

    final String textString = textView.getText().toString();

    final TextPaint textPaint = textView.getPaint();

    final SpannableStringBuilder builder = new SpannableStringBuilder();

    textView.post(() -> {

        if (!isJustify.get()) {

            final int lineCount = textView.getLineCount();
            final int textViewWidth = textView.getWidth() - 1;

            for (int i = 0; i < lineCount; i++) {

                int lineStart = textView.getLayout().getLineStart(i);
                int lineEnd = textView.getLayout().getLineEnd(i);

                String lineString = textString.substring(lineStart, lineEnd);

                if (i == lineCount - 1) {
                    builder.append(new SpannableString(lineString));
                    break;
                }

                String trimSpaceText = lineString.trim();
                String removeSpaceText = lineString.replaceAll(" ", "");

                float removeSpaceWidth = textPaint.measureText(removeSpaceText);
                float spaceCount = trimSpaceText.length() - removeSpaceText.length();

                float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount;

                SpannableString spannableString = new SpannableString(lineString);
                for (int j = 0; j < trimSpaceText.length(); j++) {
                    char c = trimSpaceText.charAt(j);
                    if (c == ' ') {
                        Drawable drawable = new ColorDrawable(0x00ffffff);
                        drawable.setBounds(0, 0, (int) eachSpaceWidth, 0);
                        ImageSpan span = new ImageSpan(drawable);
                        spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }

                builder.append(spannableString);
            }

            textView.setText(builder);
            isJustify.set(true);
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants