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.13k stars 991 forks source link

add text style bold,italic,underline,strike,shadow #368

Closed rtz333 closed 3 years ago

rtz333 commented 3 years ago
public class TextStyleBuilder {

// bold or italic
    public void withTextStyle(int typeface) {
        values.put(TextStyle.TEXT_STYLE, typeface);
    }

//strike or underline
    public void withTextFlag(int paintFlag) {
        values.put(TextStyle.TEXT_FLAG, paintFlag);
    }

    public void withTextShadow(TextShadow textShadow) {
        values.put(TextStyle.SHADOW, textShadow);
    }

    void applyStyle(@NonNull TextView textView) {
        for (Map.Entry<TextStyle, Object> entry : values.entrySet()) {
            switch (entry.getKey()) {
                case SIZE: {
                    final float size = (float) entry.getValue();
                    applyTextSize(textView, size);
                }
                break;

                case COLOR: {
                    final int color = (int) entry.getValue();
                    applyTextColor(textView, color);
                }
                break;

                case FONT_FAMILY: {
                    final Typeface typeface = (Typeface) entry.getValue();
                    applyFontFamily(textView, typeface);
                }
                break;

                case TEXT_STYLE: {
                    final int typeface = (int) entry.getValue();
                    applyTextStyle(textView, typeface);
                }
                break;
                case TEXT_FLAG: {
                    int flag = (int) entry.getValue();
                    applyTextFlag(textView, flag);
                }
                break;

                case GRAVITY: {
                    final int gravity = (int) entry.getValue();
                    applyGravity(textView, gravity);
                }
                break;

                case BACKGROUND: {
                    if (entry.getValue() instanceof Drawable) {
                        final Drawable bg = (Drawable) entry.getValue();
                        applyBackgroundDrawable(textView, bg);

                    } else if (entry.getValue() instanceof Integer) {
                        final int color = (Integer) entry.getValue();
                        applyBackgroundColor(textView, color);
                    }
                }
                break;

                case TEXT_APPEARANCE: {
                    if (entry.getValue() instanceof Integer) {
                        final int styleAppearance = (Integer) entry.getValue();
                        applyTextAppearance(textView, styleAppearance);
                    }
                }
                break;

                case SHADOW: {
                    if (entry.getValue() instanceof TextShadow){
                        TextShadow textShadow=(TextShadow) entry.getValue();
                        applyTextShadow(textView,textShadow);
                    }

                }

            }
        }
    }

    // shadow
    protected void applyTextShadow(TextView textView, TextShadow textShadow) {
        textView.setShadowLayer(textShadow.getRadius(), textShadow.getDx(), textShadow.getDy(), textShadow.getColor());
    }
    // bold or italic
    protected void applyTextStyle(TextView textView, int typeface) {
        textView.setTypeface(null, typeface);
    }

    // underline or strike
    protected void applyTextFlag(TextView textView, int flag) {
        textView.getPaint().setFlags(flag);
    }

    protected enum TextStyle {
        SIZE("TextSize"),
        COLOR("TextColor"),
        GRAVITY("Gravity"),
        FONT_FAMILY("FontFamily"),
        BACKGROUND("Background"),
        TEXT_APPEARANCE("TextAppearance"),
        TEXT_STYLE("TextStyle"),
        TEXT_FLAG("TextFlag"),
        SHADOW("Shadow");

        TextStyle(String property) {
            this.property = property;
        }

        private String property;

        public String getProperty() {
            return property;
        }
    }
}
package ja.burhanrashid52.photoeditor;

public class TextShadow {
    float radius;
    float dx;
    float dy;
    int color;

    public TextShadow(float radius, float dx, float dy, int color) {
        this.radius = radius;
        this.dx = dx;
        this.dy = dy;
        this.color = color;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public float getDx() {
        return dx;
    }

    public void setDx(float dx) {
        this.dx = dx;
    }

    public float getDy() {
        return dy;
    }

    public void setDy(float dy) {
        this.dy = dy;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }
}
rtz333 commented 3 years ago

usage: new TextStyleBuilder() .withTextColor(123) .withTextSize(12f) .withGravity(3) .withTextFont(Typeface.DEFAULT) .withBackgroundColor(321) .withTextAppearance(144) .withTextStyle(....) .withTextFlag(...) .withTextShadow(...); .applyStyle(textView);

burhanrashid52 commented 3 years ago

Can you please add some description to the issue or explain the usecase ?

rtz333 commented 3 years ago

I just added a few features to this project. Use case:

TextStyleBuilder textStyleBuilder=new TextStyleBuilder();

TextShadow textShadow=new TextShadow(2,2,2,ContextCompat.getColor(EditImageActivity.this,R.color.red_orange_color_picker)); textStyleBuilder.withTextShadow(textShadow); textStyleBuilder.withTextFlag(Paint.STRIKE_THRU_TEXT_FLAG|); // text strike line

//text underline :Paint.UNDERLINE_TEXT_FLAG

burhanrashid52 commented 3 years ago

Please raise a PR for this with test. Will review it.

anderbytes commented 3 years ago

Very good. Hope it gets to main. Much better than the simple PR that I posted with only the Shadow modification.