Arduino ST7735 Word Wrap

Hello Everyone!

I'm still pretty new to programming and I'm trying to dip my toes into the water and learn things on my own, but I'm having some trouble with what seems to me like an easy issue.

I'm trying to output some text to a ST7735 and I'm looking for an easy way for it to automatically wrap lines without splitting words. I've been scrubbing through Google to find some documentation on the subject, but surely I'm missing something

Below, I've included what I'm currently using. Can someone point me in the right direction? Is there an easy way I'm missing?

#include <TFT.h>

#define cs 10
#define dc 8
#define rst 9

TFT screen = TFT(cs, dc, rst);



void setup() {
  // put your setup code here, to run once:
  screen.begin();

  screen.background(255, 255, 255);
  screen.stroke(50, 50, 50);
  screen.setTextSize(1);
  screen.text("So then this would be how\nI would have to make a \ndocument to make sure that\neverything is on its own\nline.", 5, 5);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Yes, it's easy.

You know the number of characters that can go on your line, right? You can count the length of the word you want to put on the line. Each new word, subtract it's length from the line length. If the length of the new word is less than the available space, go to the next line and start over with the process.

Paul

here is a super basic code example. it pry needs a couple lines added to detect if a word is bigger than the line limit.

String wrap(String s, int limit){

int space = 0;
 int i = 0;
 int line = 0;
 while(i<s.length()){
  
  if(s.substring(i,i+1)==" "){space=i; }
  if(line>limit-1){s=s.substring(0,space)+"~"+s.substring(space+1);line = 0;}
  i++;line++;}
 s.replace("~","\n");
  return s;
  }

it just runs down the string remembering where the spaces are. when your line count goes past the limit it just add a line break at the last space and resets

Your code was just what I needed! Thank you!

bwente:
Your code was just what I needed! Thank you!

Maybe after an entire year, you should send a PM instead of trying to thank them here, they are probably long gone.