Skip to content

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

Open

Description

@icstar

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?

Activity

  1. aci9evil commented on Mar 20, 2021

    @aci9evil

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions