multi format in LabelField - BlackBerry

170 views Asked by At

I am placing text in a LabelField as multi-line. This is the method I am calling:

public void setFirstName(String fvalue,String lvalue,String date,String lastStatus) 
        {
            _fnameLabel.setText(fvalue+" "+lvalue+"\n"+date+"\n"+lastStatus);
            _fnameLabel.setFont((_fnameLabel.getFont().derive(Font.BOLD, 20, Ui.UNITS_px,
                    Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT)));
            _fnameLabel.setMargin(10, 0, 20, 0); //To leave some space from top and bottom
        }

I want different font (size,style etc) for each line. Is it possible to have multi-format in one labelfield. Please help.

1

There are 1 answers

11
Ted Hopp On

You can't do that with a LabelField, only with a RichTextField. You have to build arrays of offsets, fonts, and font indices to use for the field. It's a bit tedious, but not particularly hard. The method you want to use is

setText(String text, int[] offsets, byte[] attributes, Font[] fonts)

where:

  • offsets is a sorted array of positions where formatting changes, including the start (0) and end (text.length()) of the text;
  • attributes is an array of indexes into the fonts array, one for each text region (offsets.length == attributes.length + 1);
  • and fonts is the array of different fonts you want to use.

The attributes array can contain the same value more than once, so fonts.length can be anything at all, as long as every element of attributes is a valid index of a non-null element of fonts.