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
Highlighted
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel
Accepted Solution

how to prevent text from being cropped while drawing

Hi,

  I have  to draw a long Text in an area of limited width (say half the width of screen)... when i draw it using graphics.drawText() method, it gets cropped. It draws that much text only which comes within that width. I want my text to be drawn in full. text should come into next line(width is still the same) and so on.... How can i implement it?

 

Thanks and Warm Regards.

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to prevent text from being cropped while drawing

implementing that is very tricky. you can search the forum for line break algorithms or write your own.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: how to prevent text from being cropped while drawing

Hi All,

 Thanks a lot Simon.

 

     I found a very good source code for this....I am posting this code for other users help...

 

public static Vector wrapText (String text, int width) 
	{
	    Vector result = new Vector ();
	    String remaining = text;
	    while (remaining.length()>=0)
	    {
	    	int index = getSplitIndex(remaining, width);
	    	if (index == -1)
	    		break;
	    	result.addElement(remaining.substring(0,index));
	    	remaining = remaining.substring(index);
	    	if (index == 0)
	    		break;
	    }
	    return result;
	}
	
	public static int getSplitIndex(String bigString, int width)
	{
		int index = -1;
		int lastSpace = -1;
		String smallString="";
		boolean spaceEncountered = false;
		boolean maxWidthFound = false;
		for (int i=0; i<bigString.length(); i++)
		{
			char current = bigString.charAt(i);
			smallString += current;
			if (current == ' ')
			{
				lastSpace = i;
				spaceEncountered = true;
			}
			
			//int linewidth = this.getFont().getAdvance(smallString,0,  smallString.length());
			int linewidth = 0;
			try {
				linewidth = FontFamily.forName("BBAlpha Serif").getFont(Font.BOLD, 8, Ui.UNITS_pt).getAdvance(smallString,0,  smallString.length());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			if(linewidth>width)
			{
				if (spaceEncountered)
					index = lastSpace+1;
				else
					index = i;
				maxWidthFound = true;
				break;
			}
		}
		if (!maxWidthFound)
			index = bigString.length();
		return index;
	}

 you can replace the try/catch block with the commented line(obviously after getting it uncommented)..I did this because i added this code into my project in a separate utility file. If you are writing this code into your screen itself you can use commented line in place of try/catch block...

 

This code will return a Vector comprising of substrings depending on the width of area you will provide. You can then draw these strings..

 

 

Warm Regards.

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to prevent text from being cropped while drawing

if you "find" code it is usually polite to at least link to the original post and mention the author.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: how to prevent text from being cropped while drawing

I am really very sorry... I forgot that... Actually i mentioned that "I found...." but I forgot to mention the link/name...

Find below the link...

http://supportforums.blackberry.com/t5/Java-Development/Can-drawText-wrap-text-into-multiple-lines/m...