From 4c7d2d2c66bef15494780d27c81b34a4f273b2fb Mon Sep 17 00:00:00 2001 From: Brad Erickson Date: Mon, 8 Feb 2016 14:23:39 -0800 Subject: [PATCH] Fix text emoji pane font size and vertical align Fixes #3426 Closes #5196 --- .../securesms/components/emoji/EmojiView.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/emoji/EmojiView.java b/src/org/thoughtcrime/securesms/components/emoji/EmojiView.java index 038eedb974..63b52213d6 100644 --- a/src/org/thoughtcrime/securesms/components/emoji/EmojiView.java +++ b/src/org/thoughtcrime/securesms/components/emoji/EmojiView.java @@ -3,7 +3,6 @@ package org.thoughtcrime.securesms.components.emoji; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.util.AttributeSet; @@ -17,7 +16,6 @@ public class EmojiView extends View implements Drawable.Callback { private Drawable drawable; private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); - private final Rect textBounds = new Rect(); public EmojiView(Context context) { this(context, null); @@ -54,12 +52,17 @@ public class EmojiView extends View implements Drawable.Callback { float targetFontSize = 0.75f * getHeight() - getPaddingTop() - getPaddingBottom(); paint.setTextSize(targetFontSize); paint.setColor(ResUtil.getColor(getContext(), R.attr.emoji_text_color)); - paint.getTextBounds(emoji, 0, emoji.length(), textBounds); - float overflow = textBounds.width() / (getWidth() - getPaddingLeft() - getPaddingRight()); + paint.setTextAlign(Paint.Align.CENTER); + int xPos = (canvas.getWidth() / 2); + int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)); + + float overflow = paint.measureText(emoji) / + (getWidth() - getPaddingLeft() - getPaddingRight()); if (overflow > 1f) { paint.setTextSize(targetFontSize / overflow); + yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)); } - canvas.drawText(emoji, 0.5f * (getWidth() - textBounds.width()), 0.5f * (getHeight() + textBounds.height()), paint); + canvas.drawText(emoji, xPos, yPos, paint); } }