最近做通讯录小屏机 联系人姓名显示--长度超过边界字体变小
/**
* 自定义TextView,文本内容自动调整字体大小以适应TextView的大小
* @author yzp
*/
public class AutoFitTextView extends TextView {
private Paint mTextPaint;
private float mTextSize;
public AutoFitTextView(Context context) {
super(context);
}
public AutoFitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Re size the font so the specified text fits in the text box assuming the
* text box is the specified width.
*
* @param text
* @param textWidth
*/
private void refitText(String text, int textViewWidth) {
if (text == null || textViewWidth <= 0)
return;
mTextPaint = new Paint();
mTextPaint.set(this.getPaint());
int availableTextViewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
float[] charsWidthArr = new float[text.length()];
Rect boundsRect = new Rect();
mTextPaint.getTextBounds(text, 0, text.length(), boundsRect);
int textWidth = boundsRect.width();
mTextSize = getTextSize();
while (textWidth > availableTextViewWidth) {
mTextSize -= 1;
mTextPaint.setTextSize(mTextSize);
textWidth = mTextPaint.getTextWidths(text, charsWidthArr);
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
refitText(this.getText().toString(), this.getWidth());
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持呐喊教程!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎点击右下角反馈进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。