Advertisement
Guest User

AutoScaleTextView.java

a guest
Nov 20th, 2013
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. /**
  2.  
  3.  * Mod of Andreas Krings AutoScaleTextView in order to support resizing according to the TextView height.
  4.  
  5.  * It works perfectly in all the devices I have tried (Nexus 4, HTC One, HTC Wildfire S, GT-N7102)
  6.  
  7.  *
  8.  
  9.  * @author Héctor Berlanga - www.codigojavaoracle.com/android-2/ajustar-textview-android/
  10.  
  11.  * @version 1.1
  12.  
  13.  *
  14.  
  15.  */
  16.  
  17. /**
  18.  
  19.  * A custom Text View that lowers the text size when the text is to big for the
  20.  
  21.  * TextView. Modified version of one found on stackoverflow
  22.  
  23.  *
  24.  
  25.  * @author Andreas Krings - www.ankri.de
  26.  
  27.  * @version 1.0
  28.  
  29.  *
  30.  
  31.  */
  32.  
  33. public class AutoScaleTextView extends TextView {
  34.  
  35.     private Paint textPaint;
  36.  
  37.     private float preferredTextSize;
  38.  
  39.     private float minTextSize;
  40.  
  41.     public AutoScaleTextView(Context context) {
  42.  
  43.                 this(context, null);
  44.  
  45.     }
  46.  
  47.     public AutoScaleTextView(Context context, AttributeSet attrs) {
  48.  
  49.                 super(context, attrs);
  50.  
  51.                 this.textPaint = new Paint();
  52.  
  53.                 this.minTextSize = 2;
  54.  
  55.                 this.preferredTextSize = 200;
  56.  
  57.     }
  58.  
  59.     /**
  60.  
  61.      * Set the minimum text size for this view
  62.  
  63.      *
  64.  
  65.      * @param minTextSize
  66.  
  67.      *            The minimum text size
  68.  
  69.      */
  70.  
  71.     public void setMinTextSize(float minTextSize) {
  72.  
  73.                 this.minTextSize = minTextSize;
  74.  
  75.     }
  76.  
  77.     /**
  78.  
  79.      * Resize the text so that it fits
  80.  
  81.      *
  82.  
  83.      * @param text
  84.  
  85.      *            The text. Neither <code>null</code> nor empty.
  86.  
  87.      * @param textWidth
  88.  
  89.      *            The width of the TextView. > 0
  90.  
  91.      */
  92.  
  93.     private void refitText(String text, int textWidth) {
  94.  
  95.                 this.minTextSize = 2;
  96.  
  97.                 this.preferredTextSize = 200;
  98.  
  99.                 if (textWidth <= 0 || text == null || text.length() == 0)
  100.  
  101.                         return;
  102.  
  103.                 // the width
  104.  
  105.                 int targetWidth = textWidth - this.getPaddingLeft()
  106.  
  107.                         - this.getPaddingRight();
  108.  
  109.                 int targetHeight = this.getHeight() - this.getPaddingTop()
  110.  
  111.                         - this.getPaddingBottom();
  112.  
  113.                 final float threshold = 0.5f; // How close we have to be
  114.  
  115.                 this.textPaint.set(this.getPaint());
  116.  
  117.                 while ((this.preferredTextSize - this.minTextSize) > threshold) {
  118.  
  119.                         float size = (this.preferredTextSize + this.minTextSize) / 2;
  120.  
  121.                         this.textPaint.setTextSize(size);
  122.  
  123.                         int textHeight = getTextHeight(text, this.getPaint(), targetWidth,
  124.  
  125.                                 size);
  126.  
  127.                         if (this.textPaint.measureText(text) >= targetWidth
  128.  
  129.                                 || textHeight >= targetHeight) {
  130.  
  131.                         this.preferredTextSize = size; // too big
  132.  
  133.                         } else {
  134.  
  135.                         this.minTextSize = size; // too small
  136.  
  137.                         }
  138.  
  139.                 }
  140.  
  141.                 // Use min size so that we undershoot rather than overshoot
  142.  
  143.                 this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.minTextSize);
  144.  
  145.     }
  146.  
  147.     @Override
  148.  
  149.     protected void onTextChanged(final CharSequence text, final int start,
  150.  
  151.             final int before, final int after) {
  152.  
  153.                 this.refitText(text.toString(), this.getWidth());
  154.  
  155.     }
  156.  
  157.     @Override
  158.  
  159.     protected void onSizeChanged(int width, int height, int oldwidth,
  160.  
  161.             int oldheight) {
  162.  
  163.                 if (width != oldwidth)
  164.  
  165.                         this.refitText(this.getText().toString(), width);
  166.  
  167.     }
  168.  
  169.     // Set the text size of the text paint object and use a static layout to
  170.  
  171.     // render text off screen before measuring
  172.  
  173.     private int getTextHeight(CharSequence source, TextPaint originalPaint,
  174.  
  175.             int width, float textSize) {
  176.  
  177.                 // Update the text paint object
  178.  
  179.                 // modified: make a copy of the original TextPaint object for measuring
  180.  
  181.                 // (apparently the object gets modified while measuring, see also the
  182.  
  183.                 // docs for TextView.getPaint() (which states to access it read-only)
  184.  
  185.                 TextPaint paint = new TextPaint(originalPaint);
  186.  
  187.                 // Update the text paint object
  188.  
  189.                 paint.setTextSize(textSize);
  190.  
  191.                 // Measure using a static layout
  192.  
  193.                 StaticLayout layout = new StaticLayout(source, paint, width,
  194.  
  195.                         Alignment.ALIGN_CENTER, 1.0f, 0.0f, true);
  196.  
  197.         return layout.getHeight();
  198.  
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement