Labelfield text not wrapping
The below class extends labelfield but when I display a large amount text it does'nt wrap to a new line. The text just trails across the screen. When I use LabelField the text wraps. Do I need to update the paint method?
Thanks
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
public class FCLabelField extends LabelField {
private Object text;
private Font font;
private int colour;
private long style;
public FCLabelField(Object text, long style , Font font, int c开发者_Go百科olour) {
super(text, style);
this.text = text;
this.font = font;
this.colour = colour;
}
protected void paint(Graphics graphics) {
graphics.setColor(colour);
graphics.setFont(font);
graphics.drawText(text.toString(), 0, 0, DrawStyle.HCENTER, getContentWidth());
}
}
This works -
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
public class FCLabelField extends LabelField {
private Object text;
private Font font;
private int colour;
private long style;
public FCLabelField(Object text, long style , Font font, int colour) {
super(text, style);
this.text = text;
this.colour = colour;
super.setFont(font);
}
protected void paint(Graphics graphics) {
graphics.setColor(this.colour);
super.paint(graphics);
}
}
In your first version you are overriding the paint
method and not calling the superclass' paint
method. In the second, you are, this allows the code in the base class to paint the text.
If you don't want to call the superclass' paint
method, you have to change your paint method to calculate the extent of the string you're going to draw and to split it at the appropriate points, making multiple calls to drawText
to draw each fragment separately at a different y location. That's what the paint
method in LabelField
does by default, so you need to emulate it.
When you do call the superclass paint
method, the reason setting the font on the superclass works and setting the font in your paint
method doesn't is because the superclass' paint
method is calling setFont
on the Graphics
object, overwriting what you just did in your paint
method.
精彩评论