2016-06-18 9 views
1

私は自分のEditText内のテキストのサイズを自動的に変更するこのカスタムEditTextを使用しています。しかし、それはうまく動作しますが、テキストは行を変更する前にあまりにも小さくなってしまいます。どのように私の分のテキストサイズを設定するのですか?それはラインを変更し、テキストのサイズは12SPカスタム編集テキストで最小テキストサイズを設定する方法は?

public class FontFitTextView extends EditText { 

// Attributes 
private Paint mTestPaint; 
private float defaultTextSize; 

public FontFitTextView(Context context) { 
    super(context); 
    initialize(); 
} 

public FontFitTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    initialize(); 
} 

private void initialize() { 
    mTestPaint = new Paint(); 
    mTestPaint.set(this.getPaint()); 
    defaultTextSize = getTextSize(); 
} 

/* Re size the font so the specified text fits in the text box 
* assuming the text box is the specified width. 
*/ 
private void refitText(String text, int textWidth) { 

    if (textWidth <= 0 || text.isEmpty()) 
     return; 

    int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight(); 

    // this is most likely a non-relevant call 
    if(targetWidth<=2) 
     return; 

    // text already fits with the xml-defined font size? 
    mTestPaint.set(this.getPaint()); 
    mTestPaint.setTextSize(defaultTextSize); 
    if(mTestPaint.measureText(text) <= targetWidth) { 
     this.setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultTextSize); 
     return; 
    } 

    // adjust text size using binary search for efficiency 
    float hi = defaultTextSize; 
    float lo = 2; 
    final float threshold = 0.5f; // How close we have to be 
    while (hi - lo > threshold) { 
     float size = (hi + lo)/2; 
     mTestPaint.setTextSize(size); 
     if(mTestPaint.measureText(text) >= targetWidth) 
      hi = size; // too big 
     else 
      lo = size; // too small 

    } 

    // Use lo so that we undershoot rather than overshoot 
    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int height = getMeasuredHeight(); 
    refitText(this.getText().toString(), parentWidth); 
    this.setMeasuredDimension(parentWidth, height); 
} 

@Override 
protected void onTextChanged(final CharSequence text, final int start, 
          final int before, final int after) { 
    refitText(text.toString(), this.getWidth()); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    if (w != oldw || h != oldh) { 
     refitText(this.getText().toString(), w); 
    } 
} 

}

<com.your.package.activity.widget.FontFitTextView 
android:id="@+id/my_id" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:text="My Text" 
android:textSize="60sp" /> 
+0

あなたのテストxml plsを追加することができます –

+0

@KevinWallis完了、アップデート – Cassie

答えて

0

に達した場合にのみ、あなたが唯一の例についてlo

// adjust text size using binary search for efficiency 
float hi = defaultTextSize; 
float lo = 2; 

から値を変更する必要がありますように:

// adjust text size using binary search for efficiency 
float minTextSize = defaultTextSize/2; 
float hi = defaultTextSize; 
float lo = minTextSize; 
+0

を確認してください。私はコード内で両方の行を変更する必要があります – Cassie

+0

どのように分フォントサイズ12を設定しますか?あなたのコードは動作しますが、行が変更されるまでには小さすぎます。 – Cassie

+0

@Cassie 'defaultTextSize'に応じて' minTextSize'を設定します。だからデフォルトが120のときは、それの1/10を使うことができる –

関連する問題