blackberry custom-fields labelfield

El texto no viene correctamente con Custom LabelField en Blackberry



custom-fields (2)

¿Qué hay de crear CustomLabelField simplemente extendiendo LabelField ? Entonces, el diseño, la complejidad del texto de la pintura (alineación, ajuste, consideración del estilo, etc.) y otras tareas serán realizadas por el propio LabelField si establece el bit de estilo apropiado en el constructor.

Y puede aplicar cualquier fuente en ese Field simplemente invocando setFont(Font) . El campo en sí ajustará su tamaño y dibujo. Ver los siguientes fragmentos.

Implementación de CustomLabelField:

class CustomLabelField extends LabelField { private int foregroundColor; private int backgroundColor; public CustomLabelField(String label, int foregroundColor, int backgroundColor, long style) { super(label, style); this.foregroundColor = foregroundColor; this.backgroundColor = backgroundColor; } protected void paint(Graphics graphics) { graphics.setBackgroundColor(backgroundColor); graphics.clear(); graphics.setColor(foregroundColor); super.paint(graphics); } } Ejemplo:

class MyScreen extends MainScreen { public Font getMyFont(int fontSize) { try { FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif"); return alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt); } catch (Exception e) { } return null; } public MyScreen() { CustomLabelField clf = new CustomLabelField( "Main", Color.WHITE, Color.BLACK, LabelField.ELLIPSIS); // Font setup clf.setFont(getMyFont(20)); add(clf); } }

Estoy implementando Custom LabelField en mi aplicación. Funciona bien con fuentes pequeñas, cuando aumente el tamaño de la fuente, no se mostrará correctamente. Aquí puedes verlo en esta imagen.

Puede ver en la imagen que solo muestra la mitad del texto. ¿Cómo puedo superar esto?

Aquí estoy dando mi código para LabelField personalizado. Por favor dime que estoy haciendo mal.

public class CustomLabelField extends Field { private String label; private int fontSize; private int foregroundColor; private int backgroundColor; public CustomLabelField(String label, int fontSize, int foregroundColor, int backgroundColor,long style) { super(style); this.label = label; this.foregroundColor = foregroundColor; this.backgroundColor = backgroundColor; this.fontSize = fontSize; } protected void layout(int width, int height) { setExtent(width, getFont().getHeight()); } protected void paint(Graphics graphics) { graphics.setBackgroundColor(backgroundColor); graphics.clear(); graphics.setFont(setMyFont()); graphics.setColor(foregroundColor); graphics.drawText(label, 0, 0, (int)(getStyle()& DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),getWidth() - 6); } // Get font for the label field public Font setMyFont() { try { FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif"); Font appFont = alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt); return appFont; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }


En su método de layout , está configurando la altura de su campo con la altura de fuente predeterminada actual. Esto significa que cuando dibuja usando una fuente más grande, el texto se recorta. Para resolver esto, cambie su método de diseño a:

protected void layout(int width, int height) { int fieldHeight = Ui.convertSize(fontSize, Ui.UNITS_pt, Ui.UNITS_px); setExtent(width, fieldHeight); }

Esto convierte el tamaño de fuente deseado en píxeles y lo usa para establecer la altura de su campo.