diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-11-20 20:37:25 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-11-20 20:37:25 +0100 |
commit | fdeeff6fd381d6f9d259d9ae0f440230731d2f71 (patch) | |
tree | 0cf209f819febfbe197b57043e2e4b25f01ec57d /src/main/java/de | |
parent | 29f0af37c73f6e6305bd0667247b3c9173fb6470 (diff) |
make keyword styling work in quotes
Diffstat (limited to 'src/main/java/de')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/text/QuoteSpan.java | 6 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/StylingHelper.java | 27 |
2 files changed, 24 insertions, 9 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/text/QuoteSpan.java b/src/main/java/de/pixart/messenger/ui/text/QuoteSpan.java index a24575509..e4cd5f2b5 100644 --- a/src/main/java/de/pixart/messenger/ui/text/QuoteSpan.java +++ b/src/main/java/de/pixart/messenger/ui/text/QuoteSpan.java @@ -2,6 +2,7 @@ package de.pixart.messenger.ui.text; import android.graphics.Canvas; import android.graphics.Paint; +import android.support.annotation.ColorInt; import android.text.Layout; import android.text.TextPaint; import android.text.style.CharacterStyle; @@ -49,4 +50,9 @@ public class QuoteSpan extends CharacterStyle implements LeadingMarginSpan { p.setStyle(style); p.setColor(color); } + + @ColorInt + public int getColor() { + return this.color; + } }
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/StylingHelper.java b/src/main/java/de/pixart/messenger/utils/StylingHelper.java index 70b714594..8d5e73ccf 100644 --- a/src/main/java/de/pixart/messenger/utils/StylingHelper.java +++ b/src/main/java/de/pixart/messenger/utils/StylingHelper.java @@ -45,6 +45,8 @@ import android.widget.EditText; import java.util.Arrays; import java.util.List; +import de.pixart.messenger.ui.text.QuoteSpan; + public class StylingHelper { private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList( @@ -63,17 +65,11 @@ public class StylingHelper { } } - public static void format(final Editable editable, @ColorInt int color) { - final int syntaxColor = Color.argb( - Math.round(Color.alpha(color) * 0.6f), - Color.red(color), - Color.green(color), - Color.blue(color) - ); + public static void format(final Editable editable, @ColorInt int textColor) { for (ImStyleParser.Style style : ImStyleParser.parse(editable)) { editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - editable.setSpan(new ForegroundColorSpan(syntaxColor), style.getStart(), style.getStart() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - editable.setSpan(new ForegroundColorSpan(syntaxColor), style.getEnd(), style.getEnd() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + makeKeywordOpaque(editable, style.getStart(), style.getStart() + 1, textColor); + makeKeywordOpaque(editable, style.getEnd(), style.getEnd() + 1, textColor); } } @@ -92,6 +88,19 @@ public class StylingHelper { } } + private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) { + QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class); + @ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor; + @ColorInt int keywordColor = transformColor(textColor); + editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + + private static + @ColorInt + int transformColor(@ColorInt int c) { + return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c)); + } + public static class MessageEditorStyler implements TextWatcher { private final EditText mEditText; |