Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    find width of the text

    Hi,
    I wander if it's possible to find the width of the test, which I want to display in the TextItem
    Code:
    private final static int LINK_MAX_WIDTH = 420;
    
    private void initLink( final TextItem link, final String linkTitle) {
            link.setEndRow( true );
            link.setShowTitle( false );
            link.setValue( linkTitle );
            link.setColSpan( 1 );
            link.setWidth( LINK_MAX_WIDTH );
            link.setCanEdit( false );
            link.setCanFocus( false );
    }
    Right now I use some hard-coded LINK_MAX_WIDTH value, but I wanted to find the real size of the
    Code:
    String linkTitle
    in order to set the TextItem to display only this string and not to be too big or too small for the text.
    Do I have a way to know the size of the text (in pixels obviously) ?

    #2
    In general, you can measure content by placing it with a Canvas as canvas.contents, draw()ing the Canvas, and checking scrollHeight. You need to apply the same styling as the styling used where the text actually appears.

    You can place the Canvas used for size testing offscreen at negative top coordinates. Do not use negative left coordinates or you will create horizontal scrolling in RTL mode.

    Comment

    Working...
    X