šŸ¤–Have you ever tried Chat.M5Stack.com before asking??šŸ˜Ž
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    How to center a text?

    FAQS
    3
    10
    30.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      PƩpƩMax
      last edited by

      Hello everyone,
      Quiz, I have a question, how can I do to center my text on the screen.

      My code:void Display_WiFi_Connected() {
      M5.Lcd.clear(BLACK);
      M5.Lcd.setTextSize(2);
      M5.Lcd.print("M5Stack has been connected");
      }

      I know I can use the function M5.Lcd.setCursor(); but I would like it to center my text according to its size ????
      Thanks

      C 1 Reply Last reply Reply Quote 0
      • reaper7R
        reaper7
        last edited by

        use:
        setTextDatum(uint8_t datum);

        https://github.com/m5stack/M5Stack/blob/master/src/utility/In_eSPI.h#L554

        and available options:
        https://github.com/m5stack/M5Stack/blob/master/src/utility/In_eSPI.h#L331

        MY GITHUB: https://github.com/reaper7/

        1 Reply Last reply Reply Quote 0
        • P
          PƩpƩMax
          last edited by PƩpƩMax

          Thank you so much, I did'nt see this function
          But where should I put the function in my code?

          void Display_WiFi_Connected() {
          M5.Lcd.clear(BLACK);
          M5.Lcd.setTextSize(2);
          M5.Lcd.print("M5Stack has been connected");
          }

          1 Reply Last reply Reply Quote 0
          • C
            Calin @PƩpƩMax
            last edited by Calin

            @pépémax 在 How to center a text? 中诓:

            Hello everyone,
            Quiz, I have a question, how can I do to center my text on the screen.

            My code:void Display_WiFi_Connected() {
            M5.Lcd.clear(BLACK);
            M5.Lcd.setTextSize(2);
            M5.Lcd.print("M5Stack has been connected");
            }

            I know I can use the function M5.Lcd.setCursor(); but I would like it to center my text according to its size ????
            Thanks

            Try this:

            M5.Lcd.drawCentreString("your string", x, y, font);

            So your code should be:

            M5.Lcd.drawCentreString("M5Stack has been connected", 160, 120, 2);

            to write a string size 2 on the center of the screen.

            You also have the option to write a right aligned string.

            I love reading C++ headers :)

            1 Reply Last reply Reply Quote 1
            • reaper7R
              reaper7
              last edited by

              For safe use in the future, better way is:

              void Display_WiFi_Connected() {
                M5.Lcd.clear(BLACK);
                M5.Lcd.setTextDatum(CC_DATUM);
                M5.Lcd.drawString("M5Stack has been connected", 160, 120, 2);
              }
              

              because drawCentreString is marked as deprecated

              MY GITHUB: https://github.com/reaper7/

              P 1 Reply Last reply Reply Quote 0
              • P
                PƩpƩMax @reaper7
                last edited by PƩpƩMax

                @reaper7 @Calin Thanks mens
                but my text was not center in my display

                M5.Lcd.drawString("M5Stack has been connected", 160, 120, 2);
                

                I think it's because of 160 and 120 values.
                I would like my text to be centered in height and width.

                1 Reply Last reply Reply Quote 0
                • reaper7R
                  reaper7
                  last edited by reaper7

                  what is your screen orientation? landscape or portrait ??
                  where is your text?
                  try to swap values 160 with 120 for portrait mode
                  or use this instead:

                  M5.Lcd.drawString("M5Stack has been connected", (int)(M5.Lcd.width()/2), (int)(M5.Lcd.height()/2), 2);
                  

                  which calculates values for current orientation.

                  I do not have hw for tests

                  MY GITHUB: https://github.com/reaper7/

                  1 Reply Last reply Reply Quote 0
                  • P
                    PƩpƩMax
                    last edited by

                    Excuse me but it's my fault

                    1 Reply Last reply Reply Quote 0
                    • reaper7R
                      reaper7
                      last edited by

                      but it's working or not?

                      MY GITHUB: https://github.com/reaper7/

                      1 Reply Last reply Reply Quote 0
                      • P
                        PƩpƩMax
                        last edited by

                        Yes yes it's working

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post