Blond Mobile


J2ME UI: Text wrapping on Canvas

The drawString() method that is used to write text on the canvas dose not know how to wrap the text, hence,  when using it and writing a long text, only one line will appear and the text will be cut.

In order to have the text written correctly, there is a need to implement a text wrap.

The below function writes the given ‘txt’ on the canvas’s Graphics ‘g’, starting from ‘x’ and ‘y’ coordinates, using the given ‘font’. the area in which the text should be written is ‘width’ and the text is written by the ‘alignment’ . using the alignment you can write text from left to right or from right to left

The function returns the next y coordinate on the canvas, which is the next location for a text to be written. this is good for cases that you want to continue writing on the canvas but with a different font or color.

public int write(Graphics g, String txt, int x, int y,
                         int width, Font font,
                         int alignment ){
    m_font = font;
    m_txt = txt;
    m_length = txt.length();
    m_width = width;
    //reseting
    m_position =0;
    m_start = 0;

    int fontHight = m_font.getHeight() + 1;
    String s;
    g.setFont(m_font);
    while(hasMoreLines()){
    s = nextLine().trim();
    g.drawString(s, x, y, Graphics.TOP|alignment );
    y += fontHight;
    }
    return y;
}
below is the implementation  for the  "helper" functions
private boolean hasMoreLines(){
    return (m_position<(m_length-1));
}
private String nextLine(){
     int maxLength = m_txt.length();
     int next = next();
     if(m_start>=maxLength || next>maxLength)
          return null;
     String s =m_txt.substring(m_start, next);
     m_start = next;
     if((m_txt.length()-1>m_start )&& ((m_txt.charAt(m_start)=='\n') ||
        (m_txt.charAt(m_start)==' '))){
          m_position++;
          m_start++;
     }
     return s;
}

private int next(){
     int i=getNextWord(m_position);
     int lastBreak = -1;
     String line;
     line= m_txt.substring(m_position, i);
     int lineWidth = m_font.stringWidth(line);
     while (i<m_length && lineWidth<= m_width){
         if(m_txt.charAt(i)==' ' )
            lastBreak = i;
         else if(m_txt.charAt(i)== '\n'){
            lastBreak =i;
            break;
         }
         if(++i<m_length){
            i= getNextWord(i);
            line = m_txt.substring(m_position, i);
            lineWidth = m_font.stringWidth(line);
         }
     }
     if(i==m_length && lineWidth<= m_width)
        m_position = i;
     else if(lastBreak == m_position)
        m_position++;
     else if(lastBreak < m_position)
        m_position =i;
     else
        m_position = lastBreak;
     return m_position;
  }
private int getNextWord(int startIndex){
    int space = m_txt.indexOf(' ', startIndex);
    int newLine = m_txt.indexOf('\n', startIndex);
    if(space ==-1)
       space = m_length;
    if(newLine ==-1)
       newLine = m_length;
    if(space<newLine)
       return space;
    else
      return newLine;
}

Tags: , , , ,

7 Responses to “J2ME UI: Text wrapping on Canvas”

  1. wei Says:

    can u give me the full code?

  2. xxx55512 Says:

    Hell yeah! This post sounds really good. Reading your blog is useful and interesting. Keep it that way.

  3. mamta Says:

    I’m creating a text editor… can u send me the full code please???
    Thanks

  4. ahok Says:

    thanks for the code…

    it’s work…
    it’s really work..
    it’s really really work…

    thanks a lot..

  5. Devinka Says:

    Thanks Bro

    This works fine. thaaaaaaaaaaaaaaaaaaaaaannnnnnnnnnnnnnnnnnnnnnkkkkkkkkkkkkkkkkkkkkkkkssssss

  6. Rajashekar Says:

    This is really awesome code….thanks a lot pal.

    i modified a little in the above code for increasing the GridItem’s height dynamically in Nebula Grid (SWT), based on the number of lines the text in each GridItem has occupied while wrapping the text in it.

    Thanks a lot again……

  7. Welysson Says:

    It would very nice if you post the complete code here.

    Thank you for this amazing job

Leave a Reply

Jajah is the VoIP player that brought you web-activated telephony.