Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Use the built in Widget in code or XML:
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
autofit:autofit_minTextSize="16sp"
/>
```

Expand Down
68 changes: 34 additions & 34 deletions library/src/main/java/me/grantland/widget/AutofitHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* 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_minTextSize
* @attr ref R.styleable.AutofitTextView_precision
* @attr ref R.styleable.AutofitTextView_autofit_sizeToFit
* @attr ref R.styleable.AutofitTextView_autofit_minTextSize
* @attr ref R.styleable.AutofitTextView_autofit_precision
*/
public class AutofitHelper {

Expand Down Expand Up @@ -60,36 +60,36 @@ 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 autofit_sizeToFit = true;
if (attrs != null) {
Context context = view.getContext();
int minTextSize = (int) helper.getMinTextSize();
float precision = helper.getPrecision();
int autofit_minTextSize = (int) helper.getMinTextSize();
float autofit_precision = helper.getPrecision();

TypedArray ta = context.obtainStyledAttributes(
attrs,
R.styleable.AutofitTextView,
defStyle,
0);
sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
minTextSize);
precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
autofit_sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_autofit_sizeToFit, autofit_sizeToFit);
autofit_minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_autofit_minTextSize,
autofit_minTextSize);
autofit_precision = ta.getFloat(R.styleable.AutofitTextView_autofit_precision, autofit_precision);
ta.recycle();

helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize)
.setPrecision(precision);
helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, autofit_minTextSize)
.setPrecision(autofit_precision);
}
helper.setEnabled(sizeToFit);
helper.setEnabled(autofit_sizeToFit);

return helper;
}

/**
* 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) {
private static void autofit(TextView view, TextPaint paint, float autofit_minTextSize, float maxTextSize,
int maxLines, float autofit_precision) {
if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
// Don't auto-size since there's no limit on lines.
return;
Expand Down Expand Up @@ -124,12 +124,12 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f

if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
|| getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, autofit_precision,
displayMetrics);
}

if (size < minTextSize) {
size = minTextSize;
if (size < autofit_minTextSize) {
size = autofit_minTextSize;
}

view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
Expand All @@ -139,7 +139,7 @@ private static void autofit(TextView view, TextPaint paint, float minTextSize, f
* 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,
float targetWidth, int maxLines, float low, float high, float autofit_precision,
DisplayMetrics displayMetrics) {
float mid = (low + high) / 2.0f;
int lineCount = 1;
Expand All @@ -159,14 +159,14 @@ private static float getAutofitTextSize(CharSequence text, TextPaint paint,

if (lineCount > maxLines) {
// For the case that `text` has more newline characters than `maxLines`.
if ((high - low) < precision) {
if ((high - low) < autofit_precision) {
return low;
}
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision,
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, autofit_precision,
displayMetrics);
}
else if (lineCount < maxLines) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision,
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, autofit_precision,
displayMetrics);
}
else {
Expand All @@ -181,13 +181,13 @@ else if (lineCount < maxLines) {
}
}

if ((high - low) < precision) {
if ((high - low) < autofit_precision) {
return low;
} else if (maxLineWidth > targetWidth) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision,
return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, autofit_precision,
displayMetrics);
} else if (maxLineWidth < targetWidth) {
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision,
return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, autofit_precision,
displayMetrics);
} else {
return mid;
Expand Down Expand Up @@ -280,22 +280,22 @@ public AutofitHelper removeOnTextSizeChangeListener(OnTextSizeChangeListener lis
}

/**
* Returns the amount of precision used to calculate the correct text size to fit within its
* Returns the amount of autofit_precision used to calculate the correct text size to fit within its
* bounds.
*/
public float getPrecision() {
return mPrecision;
}

/**
* Set the amount of precision used to calculate the correct text size to fit within its
* bounds. Lower precision is more precise and takes more time.
* Set the amount of autofit_precision used to calculate the correct text size to fit within its
* bounds. Lower autofit_precision is more precise and takes more time.
*
* @param precision The amount of precision.
* @param autofit_precision The amount of autofit_precision.
*/
public AutofitHelper setPrecision(float precision) {
if (mPrecision != precision) {
mPrecision = precision;
public AutofitHelper setPrecision(float autofit_precision) {
if (mPrecision != autofit_precision) {
mPrecision = autofit_precision;

autofit();
}
Expand All @@ -315,7 +315,7 @@ public float getMinTextSize() {
*
* @param size The scaled pixel size.
*
* @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
* @attr ref me.grantland.R.styleable#AutofitTextView_autofit_minTextSize
*/
public AutofitHelper setMinTextSize(float size) {
return setMinTextSize(TypedValue.COMPLEX_UNIT_SP, size);
Expand All @@ -328,7 +328,7 @@ public AutofitHelper setMinTextSize(float size) {
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
* @attr ref me.grantland.R.styleable#AutofitTextView_autofit_minTextSize
*/
public AutofitHelper setMinTextSize(int unit, float size) {
Context context = mTextView.getContext();
Expand Down
26 changes: 13 additions & 13 deletions library/src/main/java/me/grantland/widget/AutofitLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* 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_minTextSize
* @attr ref R.styleable.AutofitTextView_precision
* @attr ref R.styleable.AutofitTextView_autofit_sizeToFit
* @attr ref R.styleable.AutofitTextView_autofit_minTextSize
* @attr ref R.styleable.AutofitTextView_autofit_precision
*/
public class AutofitLayout extends FrameLayout {

Expand All @@ -42,26 +42,26 @@ public AutofitLayout(Context context, AttributeSet attrs, int defStyle) {
}

private void init(Context context, AttributeSet attrs, int defStyle) {
boolean sizeToFit = true;
int minTextSize = -1;
float precision = -1;
boolean autofit_sizeToFit = true;
int autofit_minTextSize = -1;
float autofit_precision = -1;

if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(
attrs,
R.styleable.AutofitTextView,
defStyle,
0);
sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
minTextSize);
precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
autofit_sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_autofit_sizeToFit, autofit_sizeToFit);
autofit_minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_autofit_minTextSize,
autofit_minTextSize);
autofit_precision = ta.getFloat(R.styleable.AutofitTextView_autofit_precision, autofit_precision);
ta.recycle();
}

mEnabled = sizeToFit;
mMinTextSize = minTextSize;
mPrecision = precision;
mEnabled = autofit_sizeToFit;
mMinTextSize = autofit_minTextSize;
mPrecision = autofit_precision;
}

@Override
Expand Down
30 changes: 15 additions & 15 deletions library/src/main/java/me/grantland/widget/AutofitTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
/**
* 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_minTextSize
* @attr ref R.styleable.AutofitTextView_precision
* @attr ref R.styleable.AutofitTextView_autofit_sizeToFit
* @attr ref R.styleable.AutofitTextView_autofit_minTextSize
* @attr ref R.styleable.AutofitTextView_autofit_precision
*/
public class AutofitTextView extends TextView implements AutofitHelper.OnTextSizeChangeListener {

Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean isSizeToFit() {
}

/**
* Sets the property of this field (sizeToFit), to automatically resize the text to fit its
* Sets the property of this field (autofit_sizeToFit), to automatically resize the text to fit its
* constraints.
*/
public void setSizeToFit() {
Expand All @@ -97,10 +97,10 @@ public void setSizeToFit() {
* 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 autofit_sizeToFit
*/
public void setSizeToFit(boolean sizeToFit) {
mHelper.setEnabled(sizeToFit);
public void setSizeToFit(boolean autofit_sizeToFit) {
mHelper.setEnabled(autofit_sizeToFit);
}

/**
Expand Down Expand Up @@ -148,7 +148,7 @@ public float getMinTextSize() {
*
* @param minSize The scaled pixel size.
*
* @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
* @attr ref me.grantland.R.styleable#AutofitTextView_autofit_minTextSize
*/
public void setMinTextSize(int minSize) {
mHelper.setMinTextSize(TypedValue.COMPLEX_UNIT_SP, minSize);
Expand All @@ -161,28 +161,28 @@ public void setMinTextSize(int minSize) {
* @param unit The desired dimension unit.
* @param minSize The desired size in the given units.
*
* @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
* @attr ref me.grantland.R.styleable#AutofitTextView_autofit_minTextSize
*/
public void setMinTextSize(int unit, float minSize) {
mHelper.setMinTextSize(unit, minSize);
}

/**
* Returns the amount of precision used to calculate the correct text size to fit within its
* Returns the amount of autofit_precision used to calculate the correct text size to fit within its
* bounds.
*/
public float getPrecision() {
return mHelper.getPrecision();
}

/**
* Set the amount of precision used to calculate the correct text size to fit within its
* bounds. Lower precision is more precise and takes more time.
* Set the amount of autofit_precision used to calculate the correct text size to fit within its
* bounds. Lower autofit_precision is more precise and takes more time.
*
* @param precision The amount of precision.
* @param autofit_precision The amount of autofit_precision.
*/
public void setPrecision(float precision) {
mHelper.setPrecision(precision);
public void setPrecision(float autofit_precision) {
mHelper.setPrecision(autofit_precision);
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

<declare-styleable name="AutofitTextView">
<!-- Minimum size of the text. -->
<attr name="minTextSize" format="dimension" />
<!-- Amount of precision used to calculate the correct text size to fit within its
bounds. Lower precision is more precise and takes more time. -->
<attr name="precision" format="float" />
<attr name="autofit_minTextSize" format="dimension" />
<!-- Amount of autofit_precision used to calculate the correct text size to fit within its
bounds. Lower autofit_precision is more precise and takes more time. -->
<attr name="autofit_precision" format="float" />
<!-- Defines whether to automatically resize text to fit to the view's bounds. -->
<attr name="sizeToFit" format="boolean" />
<attr name="autofit_sizeToFit" format="boolean" />
</declare-styleable>
</resources>
2 changes: 1 addition & 1 deletion sample/src/main/res/layout-land/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
android:textSize="50sp"
android:gravity="center"
android:singleLine="true"
autofit:minTextSize="8sp"
autofit:autofit_minTextSize="8sp"
/>
</LinearLayout>
</LinearLayout>
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
android:textSize="50sp"
android:gravity="center"
android:singleLine="true"
autofit:minTextSize="8sp"
autofit:autofit_minTextSize="8sp"
/>
</LinearLayout>
</ScrollView>