-
-
Save sugimotoak/4314998 to your computer and use it in GitHub Desktop.
既存のサイズとは関係なく、最大サイズで表示する。
最大サイズは34Fとする。
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; | |
/** 最大のテキストサイズ **/ | |
private static final float MAX_TEXT_SIZE = 34f; | |
/** | |
* コンストラクタ | |
* @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(); | |
} | |
/** | |
* テキストサイズ調整 | |
*/ | |
public void resize() { | |
Paint paint = new Paint(); | |
// Viewの幅 | |
int viewWidth = this.getWidth(); | |
// テキストサイズ | |
float textSize = MAX_TEXT_SIZE; | |
// Paintにテキストサイズ設定 | |
paint.setTextSize(textSize); | |
// テキストの横幅取得 | |
float textWidth = paint.measureText(this.getText().toString()); | |
while (viewWidth < textWidth) { | |
// 横幅に収まるまでループ | |
if (MIN_TEXT_SIZE >= textSize) { | |
// 最小サイズ以下になる場合は最小サイズ | |
textSize = MIN_TEXT_SIZE; | |
break; | |
} | |
// テキストサイズをデクリメント | |
textSize--; | |
// Paintにテキストサイズ設定 | |
paint.setTextSize(textSize); | |
// テキストの横幅を再取得 | |
textWidth = paint.measureText(this.getText().toString()); | |
} | |
// テキストサイズ設定 | |
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment