private static int getMaxLines(AppCompatTextView view) { int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit) TransformationMethod method = view.getTransformationMethod(); if (method != null && method instanceof SingleLineTransformationMethod) { maxLines = 1; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // setMaxLines() and getMaxLines() are only available on android-16+ maxLines = view.getMaxLines(); } return maxLines; }
@Override public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { final TransformationMethod transformationMethod = isChecked ? null : PasswordTransformationMethod.getInstance(); for (final EditText passwordView : passwordViews) passwordView.setTransformationMethod(transformationMethod); }
/** * Sets the transformation that is applied to the text that this * TextView is displaying. * * @attr ref android.R.styleable#TextView_password * @attr ref android.R.styleable#TextView_singleLine */ public final void setTransformationMethod(TransformationMethod method) { if (method == mTransformation) { // Avoid the setText() below if the transformation is // the same. return; } if (mTransformation != null) { if (mText instanceof Spannable) { ((Spannable) mText).removeSpan(mTransformation); } } mTransformation = method; setText(mText); }
private static int getMaxLines(TextView view) { int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit) TransformationMethod method = view.getTransformationMethod(); if (method != null && method instanceof SingleLineTransformationMethod) { maxLines = 1; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // setMaxLines() and getMaxLines() are only available on android-16+ maxLines = view.getMaxLines(); } return maxLines; }
static ViewAssertion isPasswordToggledVisible(final boolean isToggledVisible) { return new ViewAssertion() { @Override public void check(View view, NoMatchingViewException noViewFoundException) { assertTrue(view instanceof TextInputLayout); EditText editText = ((TextInputLayout) view).getEditText(); TransformationMethod transformationMethod = editText.getTransformationMethod(); if (isToggledVisible) { assertNull(transformationMethod); } else { assertEquals(PasswordTransformationMethod.getInstance(), transformationMethod); } } }; }
/** * Method that catch the text transformation (e.g. uppercase tranformation) * and return the transformed text * @param text the text to be transformed * @return transformed text */ private String getTranformedText(String text) { // Check if text has undergone transformation TransformationMethod transformationMethod = getTransformationMethod(); if (transformationMethod != null) { // Get the tranformation text = transformationMethod.getTransformation(text, this).toString(); } return text; }
/** * Re size the font so the specified text fits in the text box assuming the text box is the * specified width. */ private void refitText() { CharSequence text = getText(); if (TextUtils.isEmpty(text)) { return; } TransformationMethod method = getTransformationMethod(); if (method != null) { text = method.getTransformation(text, this); } int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight(); if (targetWidth > 0) { float high = 100; float low = 0; mPaint.set(getPaint()); mPaint.setTextSize(getTextSize()); float letterSpacing = getLetterSpacing(text, mPaint, targetWidth, low, high, mPrecision); mPaint.setLetterSpacing(letterSpacing); calculateSections(text); super.setLetterSpacing(letterSpacing); } }
public MaterialAutoCompleteTextView(Context context, AttributeSet attrs, int style) { super(context, attrs, style); setFocusable(true); setFocusableInTouchMode(true); setClickable(true); floatingLabelTextSize = getResources().getDimensionPixelSize(R.dimen.floating_label_text_size); innerComponentsSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing); bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, Color.BLACK); ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); setTextColor(colorStateList); primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, baseColor); setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0)); errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, Color.parseColor("#e7492E")); maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0); singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false); typedArray.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(null); } else { setBackgroundDrawable(null); } if (singleLineEllipsis) { TransformationMethod transformationMethod = getTransformationMethod(); setSingleLine(); setTransformationMethod(transformationMethod); } initPadding(); initText(); initFloatingLabel(); }
public MaterialEditText(Context context, AttributeSet attrs, int style) { super(context, attrs, style); setFocusable(true); setFocusableInTouchMode(true); setClickable(true); floatingLabelTextSize = getResources().getDimensionPixelSize(R.dimen.floating_label_text_size); innerComponentsSpacing = getResources().getDimensionPixelSize(R.dimen.inner_components_spacing); bottomEllipsisSize = getResources().getDimensionPixelSize(R.dimen.bottom_ellipsis_height); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText); baseColor = typedArray.getColor(R.styleable.MaterialEditText_baseColor, Color.BLACK); ColorStateList colorStateList = new ColorStateList(new int[][]{new int[]{android.R.attr.state_enabled}, EMPTY_STATE_SET}, new int[]{baseColor & 0x00ffffff | 0xdf000000, baseColor & 0x00ffffff | 0x44000000}); setTextColor(colorStateList); primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, baseColor); setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0)); errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, R.styleable.MaterialEditText_BaseErrorColor); maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0); singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false); regularExpression = typedArray.getString(R.styleable.MaterialEditText_regexExpression); regexErrorText = typedArray.getString(R.styleable.MaterialEditText_regexErrorText); regexText = typedArray.getString(R.styleable.MaterialEditText_regexText); typedArray.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(null); } else { setBackgroundDrawable(null); } if (singleLineEllipsis) { TransformationMethod transformationMethod = getTransformationMethod(); setSingleLine(); setTransformationMethod(transformationMethod); } initPadding(); initText(); initFloatingLabel(); }
/** * Sets the transformation that is applied to the text that this * TextView is displaying. * * @attr ref android.R.styleable#TextView_password * @attr ref android.R.styleable#TextView_singleLine */ public final void setTransformationMethod(TransformationMethod method) { if (method == mTransformation) { // Avoid the setText() below if the transformation is // the same. return; } if (mTransformation != null) { if (mText instanceof Spannable) { ((Spannable) mText).removeSpan(mTransformation); } } mTransformation = method; if (method instanceof TransformationMethod2) { TransformationMethod2 method2 = (TransformationMethod2) method; mAllowTransformationLengthChange = !isTextSelectable() && !(mText instanceof Editable); method2.setLengthChangesAllowed(mAllowTransformationLengthChange); } else { mAllowTransformationLengthChange = false; } setText(mText); if (hasPasswordTransformationMethod()) { notifyViewAccessibilityStateChangedIfNeeded( AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); } }
/** * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View. */ private static void autofit(AppCompatTextView view, TextPaint paint, float minTextSize, float maxTextSize, int maxLines, float precision) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; } int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight(); if (targetWidth <= 0) { return; } CharSequence text = view.getText(); TransformationMethod method = view.getTransformationMethod(); if (method != null) { text = method.getTransformation(text, view); } Context context = view.getContext(); Resources r = Resources.getSystem(); DisplayMetrics displayMetrics; float size = maxTextSize; float high = size; float low = 0; if (context != null) { r = context.getResources(); } displayMetrics = r.getDisplayMetrics(); paint.set(view.getPaint()); paint.setTextSize(size); if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) { size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics); } if (size < minTextSize) { size = minTextSize; } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); }
/** * 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) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; } int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight(); if (targetWidth <= 0) { return; } CharSequence text = view.getText(); TransformationMethod method = view.getTransformationMethod(); if (method != null) { text = method.getTransformation(text, view); } Context context = view.getContext(); Resources r = Resources.getSystem(); DisplayMetrics displayMetrics; float size = maxTextSize; float high = size; float low = 0; if (context != null) { r = context.getResources(); } displayMetrics = r.getDisplayMetrics(); paint.set(view.getPaint()); paint.setTextSize(size); if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) { size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics); } if (size < minTextSize) { size = minTextSize; } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); }
/** * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View. */ private static void adjustTextSize(TextView view, TextPaint paint, float minTextSize, float maxTextSize, int maxLines, float precision) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; } int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight(); if (targetWidth <= 0) { return; } CharSequence text = view.getText(); TransformationMethod method = view.getTransformationMethod(); if (method != null) { text = method.getTransformation(text, view); } Context context = view.getContext(); Resources r = Resources.getSystem(); DisplayMetrics displayMetrics; float size = maxTextSize; float high = size; float low = 0; if (context != null) { r = context.getResources(); } displayMetrics = r.getDisplayMetrics(); paint.set(view.getPaint()); paint.setTextSize(size); if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) { size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics); } if (size < minTextSize) { size = minTextSize; } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); }
/** * 设置 EditText 的 transformationMethod */ public EditTextDialogBuilder setTransformationMethod(TransformationMethod transformationMethod) { mTransformationMethod = transformationMethod; return this; }
/** * @see TextView#setTransformationMethod(TransformationMethod) */ public W setTransformationMethod(TransformationMethod method) { mView.setTransformationMethod(method); return (W) this; }
public MyTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); // using the minimal recommended font size _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()); _maxTextSize = getTextSize(); _paint = new TextPaint(getPaint()); if (_maxLines == 0) // no value was assigned during construction _maxLines = NO_LINE_LIMIT; // prepare size tester: _sizeTester = new SizeTester() { final RectF textRect = new RectF(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public int onTestSize(final int suggestedSize, final RectF availableSpace) { _paint.setTextSize(suggestedSize); final TransformationMethod transformationMethod = getTransformationMethod(); final String text; if (transformationMethod != null) text = transformationMethod.getTransformation(getText(), MyTextView.this).toString(); else text = getText().toString(); final boolean singleLine = getMaxLines() == 1; if (singleLine) { textRect.bottom = _paint.getFontSpacing(); textRect.right = _paint.measureText(text); } else { final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Layout.Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true); // return early if we have more lines if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines()) return 1; textRect.bottom = layout.getHeight(); int maxWidth = -1; int lineCount = layout.getLineCount(); for (int i = 0; i < lineCount; i++) { int end = layout.getLineEnd(i); if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1), text.charAt(end))) return 1; if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); } //for (int i = 0; i < layout.getLineCount(); i++) // if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) // maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); textRect.right = maxWidth; } textRect.offsetTo(0, 0); if (availableSpace.contains(textRect)) // may be too small, don't worry we will find the best match return -1; // else, too big return 1; } }; _initialized = true; }
public AutoResizeTextView(@NonNull final Context context, final AttributeSet attrs, @AttrRes final int defStyle) { super(context, attrs, defStyle); // using the minimal recommended font size _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()); _maxTextSize = getTextSize(); _paint = new TextPaint(getPaint()); if (_maxLines == 0) // no value was assigned during construction _maxLines = NO_LINE_LIMIT; // prepare size tester: _sizeTester = new SizeTester() { final RectF textRect = new RectF(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public int onTestSize(final int suggestedSize, @NonNull final RectF availableSpace) { _paint.setTextSize(suggestedSize); final TransformationMethod transformationMethod = getTransformationMethod(); final String text; if (transformationMethod != null) text = transformationMethod.getTransformation(getText(), AutoResizeTextView.this).toString(); else text = getText().toString(); final boolean singleLine = getMaxLines() == 1; if (singleLine) { textRect.bottom = _paint.getFontSpacing(); textRect.right = _paint.measureText(text); } else { final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Layout.Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true); // return early if we have more lines if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines()) return 1; textRect.bottom = layout.getHeight(); int maxWidth = -1; int lineCount = layout.getLineCount(); for (int i = 0; i < lineCount; i++) { int end = layout.getLineEnd(i); if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1))) return 1; if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); } textRect.right = maxWidth; } textRect.offsetTo(0, 0); if (availableSpace.contains(textRect)) // may be too small, don't worry we will find the best match return -1; // else, too big return 1; } }; _initialized = true; }
/** * 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) { if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) { // Don't auto-size since there's no limit on lines. return; } int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight(); if (targetWidth <= 0) { return; } CharSequence text = view.getText(); TransformationMethod method = view.getTransformationMethod(); if (method != null) { text = method.getTransformation(text, view); } Context context = view.getContext(); Resources r = Resources.getSystem(); DisplayMetrics displayMetrics; float size = maxTextSize; float high = size; float low = 0; if (context != null) { r = context.getResources(); } displayMetrics = r.getDisplayMetrics(); paint.set(view.getPaint()); paint.setTextSize(size); if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) { size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision, displayMetrics); } if (mIsHeightFitting) { int targetHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingTop(); if (targetHeight <= 0) { if (size < minTextSize) { size = minTextSize; } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); return; } float textHeight = getTextHeight(text, paint, targetWidth, size); textHeight = getTextHeight(text, paint, targetWidth, size); float heightRatio = targetHeight / textHeight; float newSize = size * heightRatio; if (newSize < size) { size = newSize; } if (size < minTextSize) { size = minTextSize; } } view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); }
/** * Makes any compound drawables associated with this textview draw closer to the text. * @param button */ public static void setTightCompoundDrawables(final TextView button){ if(button!=null) button.post(new Runnable(){ @Override public void run() { final Drawable[] existingDrawables = button.getCompoundDrawables(); int width = button.getWidth(); String text = button.getText().toString(); TransformationMethod meth = button.getTransformationMethod(); if (meth!=null ){ //the text returned from getText() may be transformed before it is drawn. IE if textAllCaps is set in XML, //The text will be subsequently transformed, changing its width. So apply the transformation if available. text = meth.getTransformation(button.getText(), button).toString(); } float textWidth = button.getPaint().measureText(text); float padding=-1; if(existingDrawables[0]!=null && existingDrawables[2]!=null){ //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); padding=(width-textWidth-existingDrawables[0].getBounds().width()-existingDrawables[2].getBounds().width()-scale(SPACING_PX))/2f; } else if(existingDrawables[0]!=null ){ //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); padding=(width-textWidth-existingDrawables[0].getBounds().width()-scale(SPACING_PX))/2f; } else if(existingDrawables[2]!=null ){ //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); padding=(width-textWidth-existingDrawables[2].getBounds().width()-scale(SPACING_PX))/2f; } //no drawables, or text too wide for view if(padding<=0 || padding>(width/2f)) padding = scale(SPACING_PX); //button.setCompoundDrawables(existingDrawables[0], existingDrawables[1], existingDrawables[2], existingDrawables[3]); button.setPadding((int)(padding+0.0), button.getPaddingTop(), (int)(padding+0.0), button.getPaddingBottom()); } }); }
public AutoResizableTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); // using the minimal recommended font size _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()); _maxTextSize = getTextSize(); _paint = new TextPaint(getPaint()); if (_maxLines == 0) // no value was assigned during construction _maxLines = NO_LINE_LIMIT; // prepare size tester: sizeTester = new SizeTester() { final RectF textRect = new RectF(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public int onTestSize(final int suggestedSize, final RectF availableSPace) { _paint.setTextSize(suggestedSize); final TransformationMethod transformationMethod = getTransformationMethod(); final String text; if (transformationMethod != null) text = transformationMethod.getTransformation(getText(), AutoResizableTextView.this).toString(); else text = getText().toString(); final boolean singleLine = getMaxLines() == 1; if (singleLine) { textRect.bottom = _paint.getFontSpacing(); textRect.right = _paint.measureText(text); } else { final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, spacingMult, _spacingAdd, true); // return early if we have more lines if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines()) return 1; textRect.bottom = layout.getHeight(); int maxWidth = -1; for (int i = 0; i < layout.getLineCount(); i++) if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); textRect.right = maxWidth; } textRect.offsetTo(0, 0); if (availableSPace.contains(textRect)) // may be too small, don't worry we will find the best match return -1; // else, too big return 1; } }; _initialized = true; }
/** * Sets the transformation that is applied to the text that this TextView is displaying. */ public final void setTransformationMethod(final TransformationMethod method) { getView().setTransformationMethod(method); }
public AutoResizeTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); // using the minimal recommended font size _minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()); _maxTextSize = getTextSize(); _paint = new TextPaint(getPaint()); if (_maxLines == 0) // no value was assigned during construction _maxLines = NO_LINE_LIMIT; // prepare size tester: _sizeTester = new SizeTester() { final RectF textRect = new RectF(); @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public int onTestSize(final int suggestedSize, final RectF availableSpace) { _paint.setTextSize(suggestedSize); final TransformationMethod transformationMethod = getTransformationMethod(); final String text; if (transformationMethod != null) text = transformationMethod.getTransformation(getText(), AutoResizeTextView.this).toString(); else text = getText().toString(); final boolean singleLine = getMaxLines() == 1; if (singleLine) { textRect.bottom = _paint.getFontSpacing(); textRect.right = _paint.measureText(text); } else { final StaticLayout layout = new StaticLayout(text, _paint, _widthLimit, Alignment.ALIGN_NORMAL, _spacingMult, _spacingAdd, true); // return early if we have more lines if (getMaxLines() != NO_LINE_LIMIT && layout.getLineCount() > getMaxLines()) return 1; textRect.bottom = layout.getHeight(); int maxWidth = -1; int lineCount = layout.getLineCount(); for (int i = 0; i < lineCount; i++) { int end = layout.getLineEnd(i); if (i < lineCount - 1 && end > 0 && !isValidWordWrap(text.charAt(end - 1), text.charAt(end))) return 1; if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); } //for (int i = 0; i < layout.getLineCount(); i++) // if (maxWidth < layout.getLineRight(i) - layout.getLineLeft(i)) // maxWidth = (int) layout.getLineRight(i) - (int) layout.getLineLeft(i); textRect.right = maxWidth; } textRect.offsetTo(0, 0); if (availableSpace.contains(textRect)) // may be too small, don't worry we will find the best match return -1; // else, too big return 1; } }; _initialized = true; }
/** * Re size the font so the specified text fits in the text box assuming the text box is the * specified width. */ private void refitText() { if (!mSizeToFit) { return; } if (mMaxLines <= 0) { // Don't auto-size since there's no limit on lines. return; } CharSequence text = getText(); TransformationMethod method = getTransformationMethod(); if (method != null) { text = method.getTransformation(text, this); } int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight(); if (targetWidth > 0) { Context context = getContext(); Resources r = Resources.getSystem(); DisplayMetrics displayMetrics; float size = mMaxTextSize; float high = size; float low = 0; if (context != null) { r = context.getResources(); } displayMetrics = r.getDisplayMetrics(); mPaint.set(getPaint()); mPaint.setTextSize(size); if ((mMaxLines == 1 && mPaint.measureText(text, 0, text.length()) > targetWidth) || getLineCount(text, mPaint, size, targetWidth, displayMetrics) > mMaxLines) { size = getTextSize(text, mPaint, targetWidth, mMaxLines, low, high, mPrecision, displayMetrics); } if (size < mMinTextSize) { size = mMinTextSize; } super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); } }