burhanrashid52 / PhotoEditor

A Photo Editor library with simple, easy support for image editing using paints,text,filters,emoji and Sticker like stories.
MIT License
4.17k stars 992 forks source link

how to get current selected text view #260

Open verinder84 opened 4 years ago

verinder84 commented 4 years ago

please guide me, how can i get current selected text view. i want to change the color of text without opening the text Editor

AdhseOne commented 4 years ago

There is no method for that, its Click event only shows the edges of the text, and Long Click is the one you can handle. The other thing is that when executing the Long click you show a small space to edit the text and next to it a button to open the entire editor.

AdhseOne commented 4 years ago

por favor, guíame, ¿cómo puedo obtener la vista actual de texto seleccionado? quiero cambiar el color del texto sin abrir el editor de texto

I found a way to do it, at least as long as there is no method that allows the library. You must go through the PhotoEditorView views, assign the specific ones to not make a recursive method, even so carry out the necessary tests.

This is my code and it works fine, at least for what I'm currently doing.

(Once the view is recovered, you assign the OnClick method and a Tag to recognize that I have already added what I need. The code I use after the: photoEditor.editText (rootView, inputText, styleBuilder); But you can use it in your event onAddViewListener)

private void onAfterAdTextView() {
    try {
        for (int i = 0; i < photoEditorView.getChildCount(); i++) {
            View child = photoEditorView.getChildAt(i);
            if (child instanceof FrameLayout) {
                FrameLayout baseFrame = (FrameLayout) child;
                FrameLayout childFrame = (FrameLayout) baseFrame.getChildAt(0);
                TextView tv = (TextView) childFrame.getChildAt(0);
                if (tv.getTag() == null) {
                    tv.setTag("added");
                    //tv.setOnClickListener(clicTextView); //Do not use, read the comment below
                    tv.setTypeface(null, Typeface.BOLD);
                    tv.setTypeface(null, Typeface.ITALIC);
                    tv.setPaintFlags(tv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    break;
                } else {
                    tv.setTypeface(null, Typeface.BOLD);
                    tv.setTypeface(null, Typeface.ITALIC);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

[EDIT] The code works correctly, but assigning the click event to the TextView does not allow natural scrolling in the View that contains it. The library uses its own Listener "MultiTouchListener", you can recover it in some way, but I don't know how much you could work it since the class is not public. I can tell you which view is assigned to the events. I simplified the code a bit to indicate the view that the TouchListener contains, I hope it can help you in something.

[EDIT 2] When I used the library I added more options for the text format, I just didn't get to upload it, I hope it helps. : D

//==== TextDisplay Class ====//
public class TextDisplay {
private String text;
private int size;
private int colorCode;
private boolean bold;
private boolean italic;
private boolean underline;
private boolean shadow;
private String fontFamily;

public TextDisplay(String text, String fontFamily, int size, @ColorInt int colorCode, boolean bold, boolean italic, boolean 
underline, boolean shadow) { /*Code*/ }

//Getters and Setters...
}

//==== AppAsset Class ====//
public static Typeface getTextFontByName(Context context, String onlyFontName) {
    String currentFont = onlyFontName.toLowerCase().replace(" ", "_") + ".ttf";
    return Typeface.createFromAsset(context.getAssets(), "fonts/" + currentFont);
}

//======  EditorActivity ======//
private void onAfterAddTextView(TextView tv, TextDisplay textDisplay) {
    try {
        final Typeface typeface = AppAsset.getTextFontByName(getApplicationContext(), textDisplay.getFontFamily());
        tv.setTag(textDisplay.getFontFamily());
        tv.setText(textDisplay.getText());
        tv.setTextSize(textDisplay.getSize());
        tv.setTextColor(textDisplay.getColorCode());
        tv.setTypeface(typeface, Typeface.NORMAL);//Asigna por defecto el estilo del texto.
        tv.setShadowLayer(0, 0, 0, Color.BLACK);
        if (textDisplay.isBold() && textDisplay.isItalic())
            tv.setTypeface(typeface, Typeface.BOLD_ITALIC);
        else {
            if (textDisplay.isBold()) tv.setTypeface(typeface, Typeface.BOLD);
            if (textDisplay.isItalic()) tv.setTypeface(typeface, Typeface.ITALIC);
        }
        if (textDisplay.isUnderline())
            tv.setPaintFlags(tv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        else
            tv.setPaintFlags(tv.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG));
        if (textDisplay.isShadow()) {
            tv.setShadowLayer(1f, 3, 3, Color.BLACK);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//======  EditorActivity ======//
@Override
public void onToolSelected(EditorToolsAdapter.ToolType toolType) {
    try {
        photoEditor.setBrushDrawingMode(false); //Desactiva el pincel y borrador.
        switch (toolType) {
            case PINCEL:
               //...
            case TEXTO:
               //... Instance textEditDialogFragment
                textEditorDialog.setOnTextEditorListener(textDisplay -> {
                    photoEditor.addText(textDisplay.getText(), textDisplay.getColorCode());
                    for (int i = 0; i < photoEditorView.getChildCount(); i++) {
                        final View rootView = photoEditorView.getChildAt(i);
                        final TextView tv = rootView.findViewById(R.id.tvPhotoEditorText);
                        if (tv != null && tv.getTag() == null) {
                            final boolean isEmoji = Character.getType(tv.getText().charAt(0)) == Character.SURROGATE;
                            if (!isEmoji) {
                                onAfterAddTextView(tv, textDisplay);
                                break;
                            }
                        }
                    }
                });
        }
    } catch (Exception e) {
        showSnackBar(e.getMessage());
    }
}

//========== EditorActivity ========== //
@Override
public void onEditTextChangeListener(View rootView, String text, int color) {
    TextView inputTextView = rootView.findViewById(R.id.tvPhotoEditorText);

    //I send this _textDisplay as a parameter to my TextEditorDialogFragment, so when it opens, apply the style that the text had
    _textDisplay = new TextDisplay(text
            , Objects.requireNonNull(inputTextView).getTag().toString()
            , currentSize, color
            , inputTextView.getTypeface().isBold()
            , inputTextView.getTypeface().isItalic()
            , inputTextView.getPaintFlags() == (inputTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG)
            , inputTextView.getShadowRadius() > 0);

    //... Instance textEditDialogFragment (Here send my _textDisplay)
    dialogFragment.setOnTextEditorListener(textDisplay -> {
         onAfterAddTextView(inputTextView, textDisplay);
    });
}