From 108edd3c3c057f038fbc20d76e2d6105816a72aa Mon Sep 17 00:00:00 2001 From: Will5 Date: Thu, 14 Jul 2016 12:15:02 -0400 Subject: [PATCH 1/7] Added height fitting option (by KonradJanica) --- .../me/grantland/widget/AutofitHelper.java | 77 ++++++++++++++++--- .../me/grantland/widget/AutofitLayout.java | 11 ++- .../me/grantland/widget/AutofitTextView.java | 25 +++++- library/src/main/res/values/attrs.xml | 2 + 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 90e0b0e..563901e 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -61,6 +61,7 @@ public static AutofitHelper create(TextView view, AttributeSet attrs) { public static AutofitHelper create(TextView view, AttributeSet attrs, int defStyle) { AutofitHelper helper = new AutofitHelper(view); boolean sizeToFit = true; + boolean heightToFit = false; if (attrs != null) { Context context = view.getContext(); int minTextSize = (int) helper.getMinTextSize(); @@ -72,14 +73,16 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty defStyle, 0); sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit); + heightToFit = ta.getBoolean(R.styleable.AutofitTextView_heightToFit, heightToFit); minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize, minTextSize); precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision); ta.recycle(); helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize) - .setPrecision(precision); + .setPrecision(precision); } + helper.setHeightFitting(heightToFit); helper.setEnabled(sizeToFit); return helper; @@ -89,7 +92,7 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View. */ private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize, - int maxLines, float precision) { + int maxLines, float precision) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; @@ -128,19 +131,50 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f displayMetrics); } - if (size < minTextSize) { - size = minTextSize; + if (mIsHeightFitting) { + int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingTop(); + if (targetHeight <= 0) { + if (size < minTextSize) { + size = minTextSize; + } + view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); + return; + } + + float textHeight = getTextHeight(text, paint, targetWidth, size); + textHeight = getTextHeight(text, paint, targetWidth, size); + float heightRatio = targetHeight / textHeight; + float newSize = size * heightRatio; + if (newSize < size) { + size = newSize; + } + + if (size < minTextSize) { + size = minTextSize; + } } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); } + /** + * Try to fit the text with current size to a static layout to calculate height needed + * by that text size. + * @note Can be put in a loop where text size is gradually decreased etc. + * @return float The height size required by the text. + */ + private static float getTextHeight(CharSequence text, TextPaint paint, int width, float textSize) { + StaticLayout textHeightAdjuster = new StaticLayout(text, paint, width, + Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); + return textHeightAdjuster.getHeight(); + } + /** * Recursive binary search to find the best size for the text. */ private static float getAutofitTextSize(CharSequence text, TextPaint paint, - float targetWidth, int maxLines, float low, float high, float precision, - DisplayMetrics displayMetrics) { + float targetWidth, int maxLines, float low, float high, float precision, + DisplayMetrics displayMetrics) { float mid = (low + high) / 2.0f; int lineCount = 1; StaticLayout layout = null; @@ -196,7 +230,7 @@ else if (lineCount < maxLines) { } private static int getLineCount(CharSequence text, TextPaint paint, float size, float width, - DisplayMetrics displayMetrics) { + DisplayMetrics displayMetrics) { paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics)); StaticLayout layout = new StaticLayout(text, paint, (int)width, @@ -234,6 +268,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { private boolean mEnabled; private boolean mIsAutofitting; + private static boolean mIsHeightFitting; private ArrayList mListeners; @@ -418,7 +453,8 @@ public AutofitHelper setMaxLines(int lines) { } /** - * Returns whether or not automatically resizing text is enabled. + * Returns whether or not automatically resizing text + * by width and number of lines is enabled. */ public boolean isEnabled() { return mEnabled; @@ -446,6 +482,27 @@ public AutofitHelper setEnabled(boolean enabled) { return this; } + /** + * Returns whether or not automatically resizing text + * by height is enabled. + * @return boolean True when height scaling is on. + */ + public boolean isHeightFitting() { + return mIsHeightFitting; + } + + /** + * Sets the state of automatically resizing by text fitting in height. + * Calls an autofit if it is already enabled. + * @param enabled The state to update the height fitting member + */ + public AutofitHelper setHeightFitting(boolean enabled) { + mIsHeightFitting = enabled; + // Fit if required + setEnabled(mEnabled); + return this; + } + /** * Returns the original text size of the View. * @@ -535,7 +592,7 @@ public void afterTextChanged(Editable editable) { private class AutofitOnLayoutChangeListener implements View.OnLayoutChangeListener { @Override public void onLayoutChange(View view, int left, int top, int right, int bottom, - int oldLeft, int oldTop, int oldRight, int oldBottom) { + int oldLeft, int oldTop, int oldRight, int oldBottom) { autofit(); } } @@ -551,4 +608,4 @@ public interface OnTextSizeChangeListener { */ public void onTextSizeChange(float textSize, float oldTextSize); } -} +} \ No newline at end of file diff --git a/library/src/main/java/me/grantland/widget/AutofitLayout.java b/library/src/main/java/me/grantland/widget/AutofitLayout.java index 7620e70..d2e5c2e 100644 --- a/library/src/main/java/me/grantland/widget/AutofitLayout.java +++ b/library/src/main/java/me/grantland/widget/AutofitLayout.java @@ -22,6 +22,7 @@ public class AutofitLayout extends FrameLayout { private boolean mEnabled; + private boolean mIsHeightFitting; private float mMinTextSize; private float mPrecision; private WeakHashMap mHelpers = new WeakHashMap(); @@ -43,6 +44,7 @@ public AutofitLayout(Context context, AttributeSet attrs, int defStyle) { private void init(Context context, AttributeSet attrs, int defStyle) { boolean sizeToFit = true; + boolean heightToFit = false; int minTextSize = -1; float precision = -1; @@ -53,6 +55,7 @@ private void init(Context context, AttributeSet attrs, int defStyle) { defStyle, 0); sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit); + heightToFit = ta.getBoolean(R.styleable.AutofitTextView_heightToFit, heightToFit); minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize, minTextSize); precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision); @@ -60,6 +63,7 @@ private void init(Context context, AttributeSet attrs, int defStyle) { } mEnabled = sizeToFit; + mIsHeightFitting = heightToFit; mMinTextSize = minTextSize; mPrecision = precision; } @@ -68,8 +72,9 @@ private void init(Context context, AttributeSet attrs, int defStyle) { public void addView(View child, int index, ViewGroup.LayoutParams params) { super.addView(child, index, params); TextView textView = (TextView) child; - AutofitHelper helper = AutofitHelper.create(textView) - .setEnabled(mEnabled); + AutofitHelper helper = AutofitHelper.create(textView); + helper.setHeightFitting(mIsHeightFitting); + helper.setEnabled(mEnabled); if (mPrecision > 0) { helper.setPrecision(mPrecision); } @@ -92,4 +97,4 @@ public AutofitHelper getAutofitHelper(TextView textView) { public AutofitHelper getAutofitHelper(int index) { return mHelpers.get(getChildAt(index)); } -} +} \ No newline at end of file diff --git a/library/src/main/java/me/grantland/widget/AutofitTextView.java b/library/src/main/java/me/grantland/widget/AutofitTextView.java index f5cd9ba..62134b7 100644 --- a/library/src/main/java/me/grantland/widget/AutofitTextView.java +++ b/library/src/main/java/me/grantland/widget/AutofitTextView.java @@ -103,6 +103,29 @@ public void setSizeToFit(boolean sizeToFit) { mHelper.setEnabled(sizeToFit); } + /** + * Returns whether or not the text will be automatically re-sized to fit its height. + */ + public boolean isHeightFitting() { + return mHelper.isHeightFitting(); + } + + /** + * Sets the property of this field (isHeightFitting), to automatically resize the text to fit + * its height. + */ + public void setHeightFitting() { + setHeightFitting(true); + } + + /** + * Enables automatic text resizing to fit the textview height + * @param isEnabled If true, the text will automatically be re-sized to fit its height + */ + public void setHeightFitting(boolean isEnabled) { + mHelper.setHeightFitting(isEnabled); + } + /** * Returns the maximum size (in pixels) of the text in this View. */ @@ -189,4 +212,4 @@ public void setPrecision(float precision) { public void onTextSizeChange(float textSize, float oldTextSize) { // do nothing } -} +} \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 15e90fe..4e1a828 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -9,5 +9,7 @@ + + \ No newline at end of file From 61020f114dd837e6ebc5bdf75e739745e2f65d67 Mon Sep 17 00:00:00 2001 From: Will5 Date: Thu, 14 Jul 2016 12:25:55 -0400 Subject: [PATCH 2/7] Addressed @brcolow's questions --- library/src/main/java/me/grantland/widget/AutofitHelper.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 563901e..4e7e658 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -132,7 +132,7 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f } if (mIsHeightFitting) { - int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingTop(); + int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom(); if (targetHeight <= 0) { if (size < minTextSize) { size = minTextSize; @@ -142,7 +142,6 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f } float textHeight = getTextHeight(text, paint, targetWidth, size); - textHeight = getTextHeight(text, paint, targetWidth, size); float heightRatio = targetHeight / textHeight; float newSize = size * heightRatio; if (newSize < size) { From 3366f587c339112d9aaecc9d3afb96bc3cb92416 Mon Sep 17 00:00:00 2001 From: Will5 Date: Sat, 23 Jul 2016 12:00:15 -0400 Subject: [PATCH 3/7] Popped `if (size < minTextSize) { size = minTextSize; }` out of it's nested conditional for non-height-fitting modes --- .../src/main/java/me/grantland/widget/AutofitHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 4e7e658..e63f23f 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -147,10 +147,10 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f if (newSize < size) { size = newSize; } + } - if (size < minTextSize) { - size = minTextSize; - } + if (size < minTextSize) { + size = minTextSize; } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); From 32a1e955502de495944bd1ddb1cd71d77236305d Mon Sep 17 00:00:00 2001 From: Will5 Date: Sat, 23 Jul 2016 12:09:03 -0400 Subject: [PATCH 4/7] Inverted `if (targetHeight <= 0)` conditional so that we don't have to duplicate minTextSize check and setTextSize calls --- .../me/grantland/widget/AutofitHelper.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index e63f23f..b90b199 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -133,19 +133,13 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f if (mIsHeightFitting) { int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom(); - if (targetHeight <= 0) { - if (size < minTextSize) { - size = minTextSize; + if (targetHeight > 0) { + float textHeight = getTextHeight(text, paint, targetWidth, size); + float heightRatio = targetHeight / textHeight; + float newSize = size * heightRatio; + if (newSize < size) { + size = newSize; } - view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); - return; - } - - float textHeight = getTextHeight(text, paint, targetWidth, size); - float heightRatio = targetHeight / textHeight; - float newSize = size * heightRatio; - if (newSize < size) { - size = newSize; } } From 9a515821e51f88ce7ebb53dc00dfc23fffb87612 Mon Sep 17 00:00:00 2001 From: Will5 Date: Sat, 23 Jul 2016 18:15:20 -0400 Subject: [PATCH 5/7] New naming convention --- .../me/grantland/widget/AutofitHelper.java | 47 ++++++++++--------- .../me/grantland/widget/AutofitLayout.java | 25 +++++----- .../me/grantland/widget/AutofitTextView.java | 35 +++++++------- library/src/main/res/values/attrs.xml | 15 +++--- 4 files changed, 64 insertions(+), 58 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index b90b199..a38b406 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -24,7 +24,8 @@ * A helper class to enable automatically resizing {@link TextView}`s {@code textSize} to fit * within its bounds. * - * @attr ref R.styleable.AutofitTextView_sizeToFit + * @attr ref R.styleable.AutofitTextView_autofitWidthEnabled + * @attr ref R.styleable.AutofitTextView_autofitHeightEnabled * @attr ref R.styleable.AutofitTextView_minTextSize * @attr ref R.styleable.AutofitTextView_precision */ @@ -60,8 +61,8 @@ public static AutofitHelper create(TextView view, AttributeSet attrs) { */ public static AutofitHelper create(TextView view, AttributeSet attrs, int defStyle) { AutofitHelper helper = new AutofitHelper(view); - boolean sizeToFit = true; - boolean heightToFit = false; + boolean autofitWidthEnabled = true; + boolean autofitHeightEnabled = false; if (attrs != null) { Context context = view.getContext(); int minTextSize = (int) helper.getMinTextSize(); @@ -72,8 +73,10 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty R.styleable.AutofitTextView, defStyle, 0); - sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit); - heightToFit = ta.getBoolean(R.styleable.AutofitTextView_heightToFit, heightToFit); + autofitWidthEnabled = ta.getBoolean(R.styleable.AutofitTextView_autofitWidthEnabled, + autofitWidthEnabled); + autofitHeightEnabled = ta.getBoolean(R.styleable.AutofitTextView_autofitHeightEnabled, + autofitHeightEnabled); minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize, minTextSize); precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision); @@ -82,8 +85,8 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize) .setPrecision(precision); } - helper.setHeightFitting(heightToFit); - helper.setEnabled(sizeToFit); + helper.setAutofitWidthEnabled(autofitWidthEnabled); + helper.setAutofitHeightEnabled(autofitHeightEnabled); return helper; } @@ -131,7 +134,7 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f displayMetrics); } - if (mIsHeightFitting) { + if (mIsAutofitHeightEnabled) { int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom(); if (targetHeight > 0) { float textHeight = getTextHeight(text, paint, targetWidth, size); @@ -259,9 +262,9 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { private float mMaxTextSize; private float mPrecision; - private boolean mEnabled; + private boolean mIsAutofitWidthEnabled; private boolean mIsAutofitting; - private static boolean mIsHeightFitting; + private static boolean mIsAutofitHeightEnabled; private ArrayList mListeners; @@ -449,18 +452,18 @@ public AutofitHelper setMaxLines(int lines) { * Returns whether or not automatically resizing text * by width and number of lines is enabled. */ - public boolean isEnabled() { - return mEnabled; + public boolean isAutofitWidthEnabled() { + return mIsAutofitWidthEnabled; } /** * Set the enabled state of automatically resizing text. */ - public AutofitHelper setEnabled(boolean enabled) { - if (mEnabled != enabled) { - mEnabled = enabled; + public AutofitHelper setAutofitWidthEnabled(boolean autofitWidthEnabled) { + if (mIsAutofitWidthEnabled != autofitWidthEnabled) { + mIsAutofitWidthEnabled = autofitWidthEnabled; - if (enabled) { + if (autofitWidthEnabled) { mTextView.addTextChangedListener(mTextWatcher); mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener); @@ -480,19 +483,19 @@ public AutofitHelper setEnabled(boolean enabled) { * by height is enabled. * @return boolean True when height scaling is on. */ - public boolean isHeightFitting() { - return mIsHeightFitting; + public boolean isAutofitHeightEnabled() { + return mIsAutofitHeightEnabled; } /** * Sets the state of automatically resizing by text fitting in height. * Calls an autofit if it is already enabled. - * @param enabled The state to update the height fitting member + * @param autofitHeightEnabled The state to update the height fitting member */ - public AutofitHelper setHeightFitting(boolean enabled) { - mIsHeightFitting = enabled; + public AutofitHelper setAutofitHeightEnabled(boolean autofitHeightEnabled) { + mIsAutofitHeightEnabled = autofitHeightEnabled; // Fit if required - setEnabled(mEnabled); + setAutofitWidthEnabled(mIsAutofitWidthEnabled); return this; } diff --git a/library/src/main/java/me/grantland/widget/AutofitLayout.java b/library/src/main/java/me/grantland/widget/AutofitLayout.java index d2e5c2e..094d18a 100644 --- a/library/src/main/java/me/grantland/widget/AutofitLayout.java +++ b/library/src/main/java/me/grantland/widget/AutofitLayout.java @@ -15,14 +15,15 @@ * A {@link ViewGroup} that re-sizes the text of it's children to be no larger than the width of the * view. * - * @attr ref R.styleable.AutofitTextView_sizeToFit + * @attr ref R.styleable.AutofitTextView_autofitWidthEnabled + * @attr ref R.styleable.AutofitTextView_autofitHeightEnabled * @attr ref R.styleable.AutofitTextView_minTextSize * @attr ref R.styleable.AutofitTextView_precision */ public class AutofitLayout extends FrameLayout { - private boolean mEnabled; - private boolean mIsHeightFitting; + private boolean mIsAutofitWidthEnabled; + private boolean mIsAutofitHeightEnabled; private float mMinTextSize; private float mPrecision; private WeakHashMap mHelpers = new WeakHashMap(); @@ -43,8 +44,8 @@ public AutofitLayout(Context context, AttributeSet attrs, int defStyle) { } private void init(Context context, AttributeSet attrs, int defStyle) { - boolean sizeToFit = true; - boolean heightToFit = false; + boolean autofitWidthEnabled = true; + boolean autofitHeightEnabled = false; int minTextSize = -1; float precision = -1; @@ -54,16 +55,18 @@ private void init(Context context, AttributeSet attrs, int defStyle) { R.styleable.AutofitTextView, defStyle, 0); - sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit); - heightToFit = ta.getBoolean(R.styleable.AutofitTextView_heightToFit, heightToFit); + autofitWidthEnabled = ta.getBoolean(R.styleable.AutofitTextView_autofitWidthEnabled, + autofitWidthEnabled); + autofitHeightEnabled = ta.getBoolean(R.styleable.AutofitTextView_autofitHeightEnabled, + autofitHeightEnabled); minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize, minTextSize); precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision); ta.recycle(); } - mEnabled = sizeToFit; - mIsHeightFitting = heightToFit; + mIsAutofitWidthEnabled = autofitWidthEnabled; + mIsAutofitHeightEnabled = autofitHeightEnabled; mMinTextSize = minTextSize; mPrecision = precision; } @@ -73,8 +76,8 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) { super.addView(child, index, params); TextView textView = (TextView) child; AutofitHelper helper = AutofitHelper.create(textView); - helper.setHeightFitting(mIsHeightFitting); - helper.setEnabled(mEnabled); + helper.setAutofitWidthEnabled(mIsAutofitWidthEnabled); + helper.setAutofitHeightEnabled(mIsAutofitHeightEnabled); if (mPrecision > 0) { helper.setPrecision(mPrecision); } diff --git a/library/src/main/java/me/grantland/widget/AutofitTextView.java b/library/src/main/java/me/grantland/widget/AutofitTextView.java index 62134b7..1a23e17 100644 --- a/library/src/main/java/me/grantland/widget/AutofitTextView.java +++ b/library/src/main/java/me/grantland/widget/AutofitTextView.java @@ -8,7 +8,8 @@ /** * A {@link TextView} that re-sizes its text to be no larger than the width of the view. * - * @attr ref R.styleable.AutofitTextView_sizeToFit + * @attr ref R.styleable.AutofitTextView_autofitWidthEnabled + * @attr ref R.styleable.AutofitTextView_autofitHeightEnabled * @attr ref R.styleable.AutofitTextView_minTextSize * @attr ref R.styleable.AutofitTextView_precision */ @@ -81,49 +82,49 @@ public AutofitHelper getAutofitHelper() { /** * Returns whether or not the text will be automatically re-sized to fit its constraints. */ - public boolean isSizeToFit() { - return mHelper.isEnabled(); + public boolean isAutofitWidthEnabled() { + return mHelper.isAutofitWidthEnabled(); } /** - * Sets the property of this field (sizeToFit), to automatically resize the text to fit its + * Sets the property of this field (autofitWidthEnabled), to automatically resize the text to fit its * constraints. */ - public void setSizeToFit() { - setSizeToFit(true); + public void setAutofitWidthEnabled() { + setAutofitWidthEnabled(true); } /** * If true, the text will automatically be re-sized to fit its constraints; if false, it will * act like a normal TextView. * - * @param sizeToFit + * @param autofitWidthEnabled */ - public void setSizeToFit(boolean sizeToFit) { - mHelper.setEnabled(sizeToFit); + public void setAutofitWidthEnabled(boolean autofitWidthEnabled) { + mHelper.setAutofitWidthEnabled(autofitWidthEnabled); } /** * Returns whether or not the text will be automatically re-sized to fit its height. */ - public boolean isHeightFitting() { - return mHelper.isHeightFitting(); + public boolean isAutofitHeightEnabled() { + return mHelper.isAutofitHeightEnabled(); } /** - * Sets the property of this field (isHeightFitting), to automatically resize the text to fit + * Sets the property of this field (autofitHeightEnabled), to automatically resize the text to fit * its height. */ - public void setHeightFitting() { - setHeightFitting(true); + public void setAutofitHeightEnabled() { + setAutofitHeightEnabled(true); } /** * Enables automatic text resizing to fit the textview height - * @param isEnabled If true, the text will automatically be re-sized to fit its height + * @param autofitHeightEnabled If true, the text will automatically be re-sized to fit its height */ - public void setHeightFitting(boolean isEnabled) { - mHelper.setHeightFitting(isEnabled); + public void setAutofitHeightEnabled(boolean autofitHeightEnabled) { + mHelper.setAutofitHeightEnabled(autofitHeightEnabled); } /** diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 4e1a828..e432549 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -1,15 +1,14 @@ - - - - - - + + + + + - + \ No newline at end of file From a0f9e0a0f8f275750e181d0d264d47f23741502a Mon Sep 17 00:00:00 2001 From: Will5 Date: Tue, 9 Aug 2016 10:45:18 -0400 Subject: [PATCH 6/7] Removed static modifier from mIsAutofitHeightEnabled --- .../src/main/java/me/grantland/widget/AutofitHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index a38b406..5521646 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -95,7 +95,7 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View. */ private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize, - int maxLines, float precision) { + int maxLines, float precision, boolean mIsAutofitWidthEnabled, boolean mIsAutofitHeightEnabled) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; @@ -264,7 +264,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { private boolean mIsAutofitWidthEnabled; private boolean mIsAutofitting; - private static boolean mIsAutofitHeightEnabled; + private boolean mIsAutofitHeightEnabled; private ArrayList mListeners; @@ -549,7 +549,7 @@ private void autofit() { float textSize; mIsAutofitting = true; - autofit(mTextView, mPaint, mMinTextSize, mMaxTextSize, mMaxLines, mPrecision); + autofit(mTextView, mPaint, mMinTextSize, mMaxTextSize, mMaxLines, mPrecision, mIsAutofitWidthEnabled, mIsAutofitHeightEnabled); mIsAutofitting = false; textSize = mTextView.getTextSize(); From f6ef8d75a888944ac883c56bb8cd3c7d16593e30 Mon Sep 17 00:00:00 2001 From: Will5 Date: Tue, 9 Aug 2016 14:51:07 -0400 Subject: [PATCH 7/7] removed member variable notation inside autofit funtion --- library/src/main/java/me/grantland/widget/AutofitHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 5521646..75f2ec9 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -95,7 +95,7 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View. */ private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize, - int maxLines, float precision, boolean mIsAutofitWidthEnabled, boolean mIsAutofitHeightEnabled) { + int maxLines, float precision, boolean isAutofitWidthEnabled, boolean isAutofitHeightEnabled) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; @@ -134,7 +134,7 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f displayMetrics); } - if (mIsAutofitHeightEnabled) { + if (isAutofitHeightEnabled) { int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom(); if (targetHeight > 0) { float textHeight = getTextHeight(text, paint, targetWidth, size);