How to change the word spacing of the processing font?

edited April 2018 in How To...

In processing, there is a line spacing function, why there is no word spacing function, so how to change the font spacing of processing font? The picture is for reference only, haha, look forward to your reply.thank you.

text

Tagged:

Answers

  • edited April 2018

    Are you trying to scale the spaces -- like changing kerning -- or are you trying to create a particular effect, like full justified text?

  • If you don't need pixel-level control, you could replace single spaces between words with double or triple spaces, etc. -- like so:

    WordSpacing--screenshot

    String unspaced, spaced;
    
    void setup() {
      size(200, 200);
      unspaced = "This is a test string.";
      spaced = stringSpace(unspaced, 3);
    }
    
    void draw() {
      background(0);
      text(stringSpace(unspaced, 1), 20, 40);
      text(stringSpace(unspaced, 2), 20, 60);
      text(spaced, 20, 100);
      text(stringSpace(unspaced, (millis()/1000)%5), 20, 140);
    }
    
    String stringSpace(String s, int times) {
      String spaces = new String(new char[times]).replace("\0", " ");
      return s.replaceAll(" ", spaces);
    }
    
  • Thanks for your reply, this is what I need.

Sign In or Register to comment.