-
-
Save esperia/cf3dbbca4a4bbb035c2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Paint; | |
import android.util.AttributeSet; | |
import android.util.TypedValue; | |
import android.widget.TextView; | |
/** | |
* サイズ自動調整TextView | |
* | |
*/ | |
public class FontFitTextView extends TextView { | |
/** 最小のテキストサイズ */ | |
private static final float MIN_TEXT_SIZE = 10f; | |
/** | |
* コンストラクタ | |
* @param context | |
*/ | |
public FontFitTextView(Context context) { | |
super(context); | |
} | |
/** | |
* コンストラクタ | |
* @param context | |
* @param attrs | |
*/ | |
public FontFitTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | |
super.onLayout(changed, left, top, right, bottom); | |
resize(); | |
} | |
/** | |
* テキストサイズ調整 | |
*/ | |
private void resize() { | |
Paint paint = new Paint(); | |
// Viewの幅 | |
int viewWidth = this.getWidth(); | |
// テキストサイズ | |
float textSize = getTextSize(); | |
// Paintにテキストサイズ設定 | |
paint.setTextSize(textSize); | |
// テキストの横幅取得 | |
float textWidth = getMaxTextWidth(paint, this.getText().toString()); | |
while (viewWidth < textWidth) { | |
// 横幅に収まるまでループ | |
if (MIN_TEXT_SIZE >= textSize) { | |
// 最小サイズ以下になる場合は最小サイズ | |
textSize = MIN_TEXT_SIZE; | |
break; | |
} | |
// テキストサイズをデクリメント | |
textSize--; | |
// Paintにテキストサイズ設定 | |
paint.setTextSize(textSize); | |
// テキストの横幅を再取得 | |
textWidth = getMaxTextWidth(paint, this.getText().toString()); | |
} | |
// テキストサイズ設定 | |
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); | |
} | |
private float getMaxTextWidth(Paint paint, String text) { | |
float maxTextWidth = 0; | |
String[] split = text.split("\n"); | |
for (String s : split) { | |
float textWidth = paint.measureText(s); | |
maxTextWidth = Math.max(maxTextWidth, textWidth); | |
} | |
return maxTextWidth; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment