Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set custom font through attributes #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
52 changes: 32 additions & 20 deletions library/src/main/java/me/grantland/widget/AutofitHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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());
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
Expand Down
6 changes: 5 additions & 1 deletion library/src/main/java/me/grantland/widget/AutofitLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ 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);
}
if (mMinTextSize > 0) {
helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize);
}
mHelpers.put(textView, helper);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
<attr name="precision" format="float" />
<!-- Defines whether to automatically resize text to fit to the view's bounds. -->
<attr name="sizeToFit" format="boolean" />
<!--Can hold custom font, typeface font name-->
<attr name="fontname" format="string" />
</declare-styleable>
</resources>
Binary file added sample/src/main/assets/fonts/Brotherina.otf
Binary file not shown.
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 @@ -60,7 +60,7 @@
android:gravity="center"
android:singleLine="true"
autofit:minTextSize="8sp"
/>
autofit:fontname="Brotherina.otf"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
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 @@ -42,6 +42,6 @@
android:gravity="center"
android:singleLine="true"
autofit:minTextSize="8sp"
/>
autofit:fontname="Brotherina.otf"/>
</LinearLayout>
</ScrollView>