diff --git a/build.gradle b/build.gradle index 794d5f6..d4389f7 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' + classpath 'com.android.tools.build:gradle:2.2.3' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 043790d..9634f3a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Apr 25 23:12:46 PDT 2016 +#Mon Feb 06 10:50:58 IST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 90e0b0e..e4e98ff 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; +import android.graphics.Typeface; import android.os.Build; import android.text.Editable; import android.text.Layout; @@ -61,6 +62,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; + String fontName = ""; + if (attrs != null) { Context context = view.getContext(); int minTextSize = (int) helper.getMinTextSize(); @@ -75,10 +78,26 @@ public static AutofitHelper create(TextView view, AttributeSet attrs, int defSty 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); + fontName = ta.getString(R.styleable.AutofitTextView_fontname); + if (fontName != null) { + try { + Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontName); + view.setTypeface(myTypeface); + + ta.recycle(); + + helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize) + .setPrecision(precision); + } catch (Exception e) { + ta.recycle(); + + helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize) + .setPrecision(precision); + + Log.d("Font not found","Please check font name or add assests/fonts folder, " + e.getMessage()); + } + } } helper.setEnabled(sizeToFit); @@ -89,7 +108,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; @@ -139,8 +158,8 @@ 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, - 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; @@ -149,7 +168,7 @@ private static float getAutofitTextSize(CharSequence text, TextPaint paint, displayMetrics)); if (maxLines != 1) { - layout = new StaticLayout(text, paint, (int)targetWidth, Layout.Alignment.ALIGN_NORMAL, + layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); lineCount = layout.getLineCount(); } @@ -164,12 +183,10 @@ private static float getAutofitTextSize(CharSequence text, TextPaint paint, } return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics); - } - else if (lineCount < maxLines) { + } else if (lineCount < maxLines) { return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics); - } - else { + } else { float maxLineWidth = 0; if (maxLines == 1) { maxLineWidth = paint.measureText(text, 0, text.length()); @@ -196,10 +213,10 @@ 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, + StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); return layout.getLineCount(); } @@ -210,8 +227,7 @@ private static int getMaxLines(TextView view) { TransformationMethod method = view.getTransformationMethod(); if (method != null && method instanceof SingleLineTransformationMethod) { maxLines = 1; - } - else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // setMaxLines() and getMaxLines() are only available on android-16+ maxLines = view.getMaxLines(); } @@ -314,7 +330,6 @@ public float getMinTextSize() { * is adjusted based on the current density and user font size preference. * * @param size The scaled pixel size. - * * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize */ public AutofitHelper setMinTextSize(float size) { @@ -327,7 +342,6 @@ 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 */ public AutofitHelper setMinTextSize(int unit, float size) { @@ -362,7 +376,6 @@ public float getMaxTextSize() { * is adjusted based on the current density and user font size preference. * * @param size The scaled pixel size. - * * @attr ref android.R.styleable#TextView_textSize */ public AutofitHelper setMaxTextSize(float size) { @@ -375,7 +388,6 @@ public AutofitHelper setMaxTextSize(float size) { * * @param unit The desired dimension unit. * @param size The desired size in the given units. - * * @attr ref android.R.styleable#TextView_textSize */ public AutofitHelper setMaxTextSize(int unit, float size) { @@ -535,7 +547,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(); } } diff --git a/library/src/main/java/me/grantland/widget/AutofitLayout.java b/library/src/main/java/me/grantland/widget/AutofitLayout.java index 7620e70..dfd01a0 100644 --- a/library/src/main/java/me/grantland/widget/AutofitLayout.java +++ b/library/src/main/java/me/grantland/widget/AutofitLayout.java @@ -68,8 +68,11 @@ 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) + AutofitHelper helper = null; + + helper = AutofitHelper.create(textView) .setEnabled(mEnabled); + if (mPrecision > 0) { helper.setPrecision(mPrecision); } @@ -77,6 +80,7 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) { helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize); } mHelpers.put(textView, helper); + } /** diff --git a/library/src/main/java/me/grantland/widget/AutofitTextView.java b/library/src/main/java/me/grantland/widget/AutofitTextView.java index f5cd9ba..ad11a58 100644 --- a/library/src/main/java/me/grantland/widget/AutofitTextView.java +++ b/library/src/main/java/me/grantland/widget/AutofitTextView.java @@ -32,8 +32,8 @@ public AutofitTextView(Context context, AttributeSet attrs, int defStyle) { } private void init(Context context, AttributeSet attrs, int defStyle) { - mHelper = AutofitHelper.create(this, attrs, defStyle) - .addOnTextSizeChangeListener(this); + mHelper = AutofitHelper.create(this, attrs, defStyle) + .addOnTextSizeChangeListener(this); } // Getters and Setters diff --git a/library/src/main/java/me/grantland/widget/exception/TypeFacedException.java b/library/src/main/java/me/grantland/widget/exception/TypeFacedException.java new file mode 100644 index 0000000..ded3fa0 --- /dev/null +++ b/library/src/main/java/me/grantland/widget/exception/TypeFacedException.java @@ -0,0 +1,11 @@ +package me.grantland.widget.exception; + +/** + * Created by Choota on 1/26/17. + */ + +public class TypeFacedException extends Exception { + public TypeFacedException(String message) { + super(message); + } +} diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 15e90fe..ccce6c5 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 diff --git a/sample/src/main/assets/fonts/Brotherina.otf b/sample/src/main/assets/fonts/Brotherina.otf new file mode 100755 index 0000000..17256f1 Binary files /dev/null and b/sample/src/main/assets/fonts/Brotherina.otf differ diff --git a/sample/src/main/res/layout-land/main.xml b/sample/src/main/res/layout-land/main.xml index 505dbab..0e90e8a 100644 --- a/sample/src/main/res/layout-land/main.xml +++ b/sample/src/main/res/layout-land/main.xml @@ -60,7 +60,7 @@ android:gravity="center" android:singleLine="true" autofit:minTextSize="8sp" - /> + autofit:fontname="Brotherina.otf"/> diff --git a/sample/src/main/res/layout/main.xml b/sample/src/main/res/layout/main.xml index cdb118e..c1b0266 100644 --- a/sample/src/main/res/layout/main.xml +++ b/sample/src/main/res/layout/main.xml @@ -42,6 +42,6 @@ android:gravity="center" android:singleLine="true" autofit:minTextSize="8sp" - /> + autofit:fontname="Brotherina.otf"/>