Escape markup / control characters
It would be nice to have a helper function for escaping markup characters in string.
For example if text is dynamically created or stored in DB and contains special symbols (_, *. \n..) which shouldn't be rendered but at the same time could have color.
The workaround is to create manually each element: new StyledText(), new NewLine()...
At first sorry for the late answer, I have been off for a while.
Are you using TextFlow? If that is the case, just use addText() instead of addMarkup(). Is that a solution for you?
Yes, it is only one way now:
paragraph.add(new StyledText(text, size, font, color));
paragraph.add(new NewLine());
Er...there are two shortcuts on Paragraph (resp. TextFlow): addText() and addMarkup(), where the latter one interprets the given text as markup, the first one doesnt.
So isn't addText() what you actually want?
Ok. Lets see an example:
String str = "custom_message_1" '; // custom user defined string stored in DB and might has underscore
String txt = String.format("{color:#FF0000} Demo: %s \n", str);
p.addMarkup(txt, BaseFont.Times);
Expected: red color: 'Demo: custom_message_1' with new line at the end Actual: red color"'Demo: custommessage1.' with new line at the end
I have the same question. When loading text from external sources (user input, DB, ...) and still be able to style the text I have to escape these special characters. For example the user can enter the headline of a PDF and this text should be printed bold and with special color.
String headline = "e = m * c²; // user input
p.addMarkup("{color:#FFAA00}*" + headline + "*", Basefont.Times);
The headline value has to be escaped. But how? There should be some static helper function.