0

given a string in resources, how can I get the bounding rectangle for it?

For plain text, I could use Paint.getTextBounds(), but this is a string from resources that has newlines and attribute settings in it.

<string name="foobar"><small>Foo</small>\nBar</string>

In other words, I'm implementing a custom view that will be displaying a string like that, and I want to compute the size of my view.

More detail: Basically, I'm implementing a variant of TextView that adjusts its font size to fit the available space rather than one that adjusts its size to fit the text.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112

2 Answers2

1

You want to show html in your custom view. Do i make it clear?

If you want to implements this all by your own code. It is a very huge work. Extending TextView and overriding some methods is the best choice, but if you insist on writing your own class, you can still get help from some system class.

Showing and measuing html should follow the code of TextView, you can find this in TextView.onDraw() and TextView.onMeasure(), here i just talk about the steps for measuring html.

  1. Parsing html. Use Html.fromHtml() to get a Spanned text.
  2. Create a StaticLayout or DynamicLayout with the spanned text.
  3. Use Layout.getLineWidth() and Layout.getHeight() to measure the text.
faylon
  • 7,360
  • 1
  • 30
  • 28
  • Yes, actually I'm overriding TextView. – Edward Falk Dec 18 '12 at 07:59
  • Thanks; I wasn't familiar with the Layout classes; looking into them now. – Edward Falk Dec 18 '12 at 08:02
  • Well, I just wasted a week being too smart for my own good. I saw that StaticLayout is instantiated from a CharSequence, but my text has style information, including font size changes, embedded in it, so something that used CharSequence wouldn't work for me. FINALLY, I realized that Spannable is also a CharSequence, and from reading the source code to StaticLayout, I realized that it gives special treatment to Spannables. Now that I have that piece of the puzzle, I realize that you've found the answer to my problem. --Thanks. – Edward Falk Dec 27 '12 at 23:50
0

Try this :

String.xml

       <string name="foobar"><small>Foo</small>\nBar</string>

In Activity

       final TextView mytext = (TextView) findViewById(R.id.my_textView);
       mytext.setText(getString(R.string.foobar));

       Button btn_getTextBounds = (Button) findViewById(R.id.ok);
       btn_getTextBounds.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               Rect rectf = new Rect();
               mytext.getLocalVisibleRect(rectf);

               Log.d("WIDTH        :",String.valueOf(rectf.width()));
               Log.d("HEIGHT       :",String.valueOf(rectf.height()));
               Log.d("left         :",String.valueOf(rectf.left));
               Log.d("right        :",String.valueOf(rectf.right));
               Log.d("top          :",String.valueOf(rectf.top));
               Log.d("bottom       :",String.valueOf(rectf.bottom));
           }
       });

It will show required attributes used by the TextView on the Visible Screen Area.

Hope it helps you.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37