Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ekke
Posts: 1,458
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

whats the recommended way to shorten strings for a given width

I know how to detect the width needed for a string using font and size.

I also know the width available for the string

 

if I detect, that the string width is > available width whats the best (or recommended) way to shorten the string ?

 

ex: "This is my example text" is too long

and perhaps "This is my examp..." fits exactly into the width and could be drawn from Graphics

 

is there API for this or do I have to reduce the length and try until its short enough ?

 

thx 4 info

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
geeneeus
Posts: 80
Registered: ‎09-12-2009
My Device: Bold 9700
My Carrier: Vodafone UK

Re: whats the recommended way to shorten strings for a given width

[ Edited ]

This is done automatically by a field.

 

It can be done by simply passing the ELLIPSES constant in to the field you are using, for example:

 

 

LabelField myLabel = new LabelField("My Really Long Label", LabelField.ELLIPSES);

 

 

Genius Development Scotland
Website: http://www.genius-dev.co.uk
Please use plain text.
Developer
ekke
Posts: 1,458
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: whats the recommended way to shorten strings for a given width

this doesnt help, because I'm drawing text of a List Row using Graphics.drawText

 

but found solution just by myself:

 

if width of text > availableWidth,

calculate width of ELLIPSIS

drawText with availableWidth - ellipsisWidth

drawText ellipsis

 

thx anyway

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
ankit
Posts: 146
Registered: ‎07-24-2008
My Device: none
My Carrier: none

Re: whats the recommended way to shorten strings for a given width

Hi

If you are drawing for yourself , the standard way is to calculate the string width for your self and append the ellipses yourself.

Please see below the code snippet which we use.

 

  public static String getTruncatedString(String string, int stringWidth,
                        net.rim.device.api.ui.Font font)
        {
                if (font.getAdvance(string) < stringWidth) {
                        return string;
                }

                final int width = font.getAdvance("...");

                final StringBuffer buffer = new StringBuffer();

                for (int i = 0; i < string.length(); i++) {
                        buffer.append(string.charAt(i));

                        if (font.getAdvance(buffer.toString()) > stringWidth - width) {
                                buffer.deleteCharAt(buffer.length() - 1);
                                break;
                        }
                }

                buffer.append("...");
                return buffer.toString();
        }

 

Thanks

Ankit

Thanks & Regards,
Ankit Dubey
Module Lead Software Engineer, mLabs
Impetus InfoTech (India) Pvt. Ltd.
Please use plain text.
Developer
ekke
Posts: 1,458
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: whats the recommended way to shorten strings for a given width

ankit,

 

thanks for clarification, that this has to be done by an own method -

have thought that there should be API from RIM for this I have overlooked ;-)

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
ekke
Posts: 1,458
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: whats the recommended way to shorten strings for a given width

BTW: I'm using

Characters.HORIZONTAL_ELLIPSIS

 instead of "..."

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
ankit
Posts: 146
Registered: ‎07-24-2008
My Device: none
My Carrier: none

Re: whats the recommended way to shorten strings for a given width

Thanks for the info.

 

:smileyhappy:

 

Ankit

Thanks & Regards,
Ankit Dubey
Module Lead Software Engineer, mLabs
Impetus InfoTech (India) Pvt. Ltd.
Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: whats the recommended way to shorten strings for a given width

There is an API for just that -- no need to mess with shortening the string yourself at all:

 

 

Graphics.drawText(text, x, y, DrawStyle.ELLIPSIS, availableWidth)

 

 

Please use plain text.
Developer
ekke
Posts: 1,458
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: whats the recommended way to shorten strings for a given width

great :smileyhappy:

 

that's exactly I was looking for

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.