1

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.

Sarah
  • 1,895
  • 2
  • 21
  • 39

1 Answers1

2

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank Ted for the reply. As I understand, offset is the position where the font change effect is to be specified. Considering I have a text:String richText = fvalue+"\n"+lvalue+"\n"+date+"\n"+lastStatus; int offsets[] ={0,5,richText.length()}; How should I specify position for the offset where the next position after "0" is the next line in the string? – Sarah Aug 05 '12 at 05:58
  • It should be like `fvalue.length() + 1`, `fvalue.length() + lvalue.length() + 2`, etc – Eugen Martynov Aug 05 '12 at 06:59
  • @Sarah - If you want the first line to be in a different font, then the offset for the change should be after the `\n` that ends the first line, which would be (like Eugen said) `fvalue.length() + 1` (instead of `5` like you have now). If `fonts[0]` is the font for the first line and `fonts[1]` is the font for the rest of the text, then declare `byte[] attributes = {0, 1}`. – Ted Hopp Aug 05 '12 at 07:50
  • Thank for the pointers. I tried like this but it throws NullPointerException. String richText = fvalue+"\n"+lvalue+"\n"+date+"\n"+lastStatus; int offsets[] = {fvalue.length()+1, fvalue.length() + lvalue.length() + 2, fvalue.length() + lvalue.length() + date.length() + 3, fvalue.length() + lvalue.length() + date.length() + lastStatus.length() +4}; byte attributes[] = {0,1,2,3}; I want to specify different font for each line. – Sarah Aug 05 '12 at 08:56
  • @Sarah - Since your attribute indices go from 0 through 3, did you define four fonts in the `fonts` array? – Ted Hopp Aug 05 '12 at 08:58
  • yes I did: Font[] fonts = new Font[4]; fonts[0] = Font.getDefault().derive(Font.BOLD, 20, Ui.UNITS_px,Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT); fonts[1] = Font.getDefault().derive(Font.BOLD, 14,Ui.UNITS_px,Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT); fonts[2] = Font.getDefault().derive(Font.ITALIC, 10,Ui.UNITS_px,Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT); fonts[3] = Font.getDefault().derive(Font.PLAIN, 14, Ui.UNITS_px,Font.ANTIALIAS_STANDARD, Font.COLORED_OUTLINE_EFFECT); – Sarah Aug 05 '12 at 08:59
  • @Sarah - If I counted correctly, `attributes.length == offsets.length`. You need to have one more offset than you have attributes (for the end of the text). – Ted Hopp Aug 05 '12 at 09:00
  • @Ted I added richText.length() as another parameter to offset: int offsets[] = {fvalue.length()+1, fvalue.length() + lvalue.length() + 2, fvalue.length() + lvalue.length() + date.length() + 3, fvalue.length() + lvalue.length() + date.length() + lastStatus.length() +4, richText.length()}; byte attributes[] = {0,1,2,3}; I still get NullPointerException. Can you please help. – Sarah Aug 05 '12 at 09:10
  • Ah, I see the problem. You need to insert `0` as the first element of `offset`. Then eliminate the entry just before `richText.length()` (it is wrong anyway; it points past the end of your text). This will leave 5 elements in `offsets` and 4 elements in `attributes` as required. – Ted Hopp Aug 05 '12 at 09:20
  • @Ted, it does not work, I keep getting Null Pointer Exception. This is what I tried: int offsets[] = {0,fvalue.length()+1, fvalue.length() + lvalue.length() + 2, fvalue.length() + lvalue.length() + date.length() + 3,richText.length()}; byte attributes[] = {0,1,2,3}; – Sarah Aug 05 '12 at 09:39
  • Can you edit your question and post the code and the exception? – Ted Hopp Aug 05 '12 at 14:41