Forcing center aligned text to be left-aligned if longer

67 Views Asked by At

Having a text part, which is basically center aligned.

text { "elm.text";
            scale: 1; 
            clip_to: "elm.clipper";
            desc { "default";
               visible: 1;
               rel1.to: "elm.clipper";
               rel2.to: "elm.clipper";
               align: 0 0.5; //NOT WORKING
               text {
                  text: "elm.text very very very long";
                  size: 24;
                  align: 0.5 0.5;
                  ellipsis: -1;
               }       
            }

Center alignment works, but I want this text to be left-aligned if it's longer than it's container.

I don't want to use min, as it ruins my layout.

I also don't want to use any scripting, as it ruins user experience.

Do I have an option to solve this very fast?

2

There are 2 best solutions below

3
wonrst On

Is there any reason not to use ellipsis?

I think, ellipsis: 0; is the simplest way.

If not, you need to create another state and do STATE_SET according to the situation.

     text { "elm.text";
        scale;
        clip_to: "elm.clipper";
        desc { "default";
           vis;
           rel.to: "elm.clipper";
           align: 0.0 0.5;
           text {
              text: "elm.text text"; // for short text
              size: 24;
              align: 0.5 0.5;
              ellipsis: 0;
           }
        }
        desc { "left_align"; // for long text
           inherit: "default" 0.0;
           text.align: 0.0 0.5;
        }
     }
1
wonrst On

I think you should use a script.

But you don't want it. Right?

I don't know any other way, but I'll share the method below.

First, set "min:1 1;" to text (the text part is increased by the length of the text.)

in script :
      public text_len = 0;
      public clipper_len = 0;

      ...
      public set_align() {
         new x, y, w, h;
         get_geometry(PART:"elm.text", x, y, w, h);
         set_int(text_len, w);
         get_geometry(PART:"elm.clipper", x, y, w, h);
         set_int(clipper_len, w);

         if (get_int(text_len) > get_int(clipper_len)) {
            run_program(PROGRAM:"set_left");
         }
         else {
            run_program(PROGRAM:"set_center");
         }
      }

The same logic can be implemented in c code, but it can be more complex.