How to wrap text?

So far I have been dealing with wrapping text by specifying its size inside 'if' statements, but there surely has to be a more convenient way. I have searched around the internet to no avail. Does anyone have a suggestions on how to wrap text inside a given space?

Tagged:

Answers

  • Look at text() reference

    You can use it with 5 parameters which holds the text in a box

  • It doesn't seem to work... Am I doing something wrong?

    int score = 1;
    
    void settings() {
      fullScreen();
    }
    void setup() {
    }
    
    void draw() {
      background(0);
      score += 1;
      fill(255,0,0);
      rect(width/4,height/4,width/2,height/2);
      fill(255);
      text("Hello",width/4,height/4,width-width/4,height-height/4);
    }
    
  • What do you expect that code to do? What does it do instead?

  • You can control the alignment of the text inside the specified text area with the textAlign() method.

  • At first I was testing whether it would display the integer 'score' as text. It did not work, then I tried with 'Hello', then creating a rectangle with size width/2 and height/2, in the middle of the screen, then wrap the string inside this rectangle. The string does get displayed, but it obviously does not wrap.

  • Obviously still does not work.

    void setup() {
      size(800,800);
    }
    void draw() {
    background(0);
    fill(255,0,0);
    rect(width/4,height/4,width/2,height/2);
    fill(255);
    
    text("Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random ",width/4,height/4,width-width/4,height-height/4);
    }
    
  • edited December 2017

    it works

    you didn't have the same last 2 paremeters in text() correct!!!

            width/2,  height/2 
    

    here it is

    void setup() {
      size(800, 800);
    }
    
    void draw() {
      background(0);
      fill(255, 0, 0);
      rect(width/4, height/4, 
        width/2, height/2);
      fill(255);
    
      String str="Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random Random ";
    
      text(str, 
        width/4, height/4, 
        width/2,  height/2 );
    }
    
  • This still does not suit my needs though.

    What I need is, for example, when I'm displaying an integer as text and I want it to be centered inside a square, it would need a different size and position depending on how many digits it has (10000 would leave the square's bounds while 10 can be pretty big without leaving them). What's the best way to do that?

  • Answer ✓

    Well, that's something else than wrap a text...

    here is a idea for the text size

    see

    https://forum.processing.org/two/discussion/20238/scale-text-to-screen-width

    public void fittedText(String text, PFont font, 
        float posX, float posY, 
        float fitX, float fitY)
    {
      textFont(font);
      textSize(min(font.getSize()*fitX/textWidth(text), fitY));
      text(text, posX, posY);
    }
    
    void setup()
    {
      size(1280, 720);
      PFont f = createFont("Arial", 7);
      background(0);
      fill(255);
      textAlign(LEFT, TOP);
      fittedText("Good to see you today!", f, 0, 0, width/4, height);
    }
    
  • edited December 2017 Answer ✓

    you can combine the two approaches I guess:

    • when the text gets longer, the size gets smaller (function fittedText above)

    • but you don't want to get it smaller than let's say 6 points.

    • from that text length onward, we don't decrease the text size anymore but we do line wrap (text() with 5 parameters)

    Chrisir

Sign In or Register to comment.