Auto Scale TextView Text to Fit within Bounds

后端 未结 30 2808
囚心锁ツ
囚心锁ツ 2020-11-21 05:49

I\'m looking for an optimal way to resize wrapping text in a TextView so that it will fit within its getHeight and getWidth bounds. I\'m not simply looking for

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 06:06

    Here is the approach I take. It's very simple. It uses successive approximation to zero in on the fontsize and can generally have it figured out in less than 10 iterations. Just replace "activityWidth" with the width of whatever view you are using to display the text in. In my example, it's set as a private field to the screen's width. The inital fontsize of 198 is only set in the event the method generates an exception (which really should never happen):

      private float GetFontSizeForScreenWidth(String text)
      {
        float fontsize = 198;
    
        try
        {
          Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
          paint.setColor(Color.RED);
          Typeface typeface = Typeface.create("Helvetica", Typeface.BOLD);
          paint.setTypeface(typeface);
          paint.setTextAlign(Align.CENTER);
    
          int lowVal = 0;
          int highVal = 2000;
          int currentVal = highVal;
    
          /*
           * Successively approximate the screen size until it is 
           * within 2 pixels of the maximum screen width. Generally
           * this will get you to the closest font size within about 10
           * iterations.
           */
    
          do
          {
            paint.setTextSize(currentVal);
            float textWidth = paint.measureText(text);
    
            float diff = activityWidth - textWidth;
    
            if ((diff >= 0) && (diff <= 2))
            {
              fontsize = paint.getTextSize();
              return fontsize;
            }
    
            if (textWidth > activityWidth)
              highVal = currentVal;
            else if (textWidth < activityWidth)
              lowVal = currentVal;
            else
            {
              fontsize = paint.getTextSize();
              return fontsize;
            }
    
            currentVal = (highVal - lowVal) / 2 + lowVal;
    
          } while (true);      
        }
        catch (Exception ex)
        {
          return fontsize;
        }
      }
    

提交回复
热议问题