This code returns string which can be shown in a field having width=width, 5 pixel margin on left and 5 pixel on right.
This can be called from
private String getTextShown( String completeString, int width ) { if ( completeString == null ) return null; width = width - 10; Font font = listfield.getFont( ); if ( font.getAdvance( completeString ) < width ) return completeString; for ( int i = completeString.length( ) - 1; i > 0; i-- ) { completeString = completeString.substring( 0, i ); StringBuffer tempBuffer = new StringBuffer( completeString ); tempBuffer.append( "..." ); String temp = tempBuffer.toString( ); if ( font.getAdvance( temp ) < width ) { return temp; } } return completeString; }
protected void paint( Graphics graphics ) { String stringToDraw = getTextShown( "This is very long text. this will not fit in one line on the blackberry device", w ); graphics.drawText( stringToDraw, x, y ); }
No comments:
Post a Comment