itzjackyscode
itzjackyscode

Reputation: 1104

How can I change how text is rendered in a TextField?

I'm creating a modified TextArea that should function like a console. To delineate output, I'm using two private-use characters: U+FF100 and U+FF101.

These characters need to be invisible when shown onscreen (i.e. like a zero-width space). I was thinking about a custom font, but I might be able to do this in my code. How do I go about doing this?

I'm using JavaFX 14.0.2.1.

Upvotes: 1

Views: 74

Answers (1)

Oliver
Oliver

Reputation: 503

RichTextFX has the InlineCssTextArea class, which allows you to add style for text ranges. You could use display: none (or the JavaFX equivalent) on the ranges.

I haven't used it, but based on the API, something like this should work:

InlineCssTextArea textArea = new InlineCssTextArea(text);

textArea.textProperty().addListener((observable, oldValue, newValue) -> {
  int index = textArea.getText().indexOf("\uDBBC\uDD00");
  while(index >= 0) {
    textArea.setStyle(0, index, index + 1, "display: none;");
  
    index = textArea.getText().indexOf("\uDBBC\uDD00", index + 1);
  }
}

You could also use a for loop if that is your preference. You would want to store the last range styled, so that when the value changes, you begin after that range.

Upvotes: 2

Related Questions