×
Namespaces

Variants
Actions
(Difference between revisions)

Moldagem de texto em Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
Line 20: Line 20:
 
}}
 
}}
  
{{Abstract|O seguinte código ilustra com moldar texto.}}
+
{{Abstract|O seguinte código demonstra como moldar texto (text wrapping).}}
  
 
<code java>
 
<code java>

Latest revision as of 19:33, 13 October 2013

Article Metadata

Artigo
Tradução:
Por valderind4
Última alteração feita por lpvalente em 13 Oct 2013

O seguinte código demonstra como moldar texto (text wrapping).

import java.util.Enumeration;
import java.util.NoSuchElementException;
 
import javax.microedition.lcdui.*;
 
//www.astrientlabs.com
public class LineEnumeration implements Enumeration {
private Font font;
private String text;
 
private int width;
private int position;
private int length;
private int start = 0;
 
public LineEnumeration(Font font, String text, int width) {
this.font = font;
this.text = text;
this.width = width;
this.length = text.length();
}
 
 
public boolean hasMoreElements() {
return (position < (length-1));
}
 
public Object nextElement() throws NoSuchElementException {
try {
return text.substring(start,(start = next()));
} catch ( IndexOutOfBoundsException e ) {
throw new NoSuchElementException(e.getMessage());
} catch ( Exception e ) {
throw new NoSuchElementException(e.getMessage());
}
}
 
 
private int next() {
int i = position;
int lastBreak = -1;
 
for ( ;i < length && font.stringWidth(text.substring(position,i)) <= width; i++ ) {
if ( text.charAt(i) == ' ' ) {
lastBreak = i;
} else if ( text.charAt(i) == '\n' ) {
lastBreak = i;
break;
}
}
 
 
if ( i == length ) {
position = i;
} else if ( lastBreak <= position ) {
position = i;
} else {
position = lastBreak;
}
 
return position;
}
}

[edit] Exemplo de uso

    LineEnumeration e = new LineEnumeration(myFont,myText,lineWidth);
while ( e.hasMoreElements() ) {
g.drawString(e.nextElement().toString(), startX,startY, Graphics.TOP | Graphics.LEFT);
startY += myFont.getHeight();
}
This page was last modified on 13 October 2013, at 19:33.
90 page views in the last 30 days.