Skip to content

Instantly share code, notes, and snippets.

@companje
Created December 9, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/d7ad4dacf47b31b69298ed5d71793eb6 to your computer and use it in GitHub Desktop.
Save companje/d7ad4dacf47b31b69298ed5d71793eb6 to your computer and use it in GitHub Desktop.
Wordwrap, ofxWordwrap
string ofxWordWrap(string input, int maxWidth, ofTrueTypeFont *font) {
vector<string> lines = ofSplitString(input,"\n");
for (int l=0; l<lines.size(); l++) {
vector<string> words = ofSplitString(lines[l]," ");
int strWidth=0;
for (int w=0; w<words.size(); w++) {
int nextWidth = font ? font->stringWidth(words[w]+"i") : words[w].length()+1;
if (strWidth+nextWidth < maxWidth) {
strWidth+=nextWidth;
} else {
strWidth=nextWidth;
words[w] = "\n" + words[w];
}
}
lines[l] = ofJoinString(words, " ");
}
return ofJoinString(lines, "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment