How do I prevent *bold* from making bold text

In the documentation:

playdate.graphics.drawText("normal *bold* _italic_", x, y)

which will output: "normal bold italic". <-- these are formatted in the docs

How do I make it so that these special characters are ignored? Users are going to be entering text, and it messes up the layout if they use these characters.

Not sure of the full context, but I can think of a few approaches. Some combination of these maybe?

  1. drawTextAligned doesn't render styles. And drawTextInRect won't either, if a font is specified to override the current font family.

  2. Replace all instances of * and _ with some unused Unicode characters on the fly, then replace those all in turn with ** and __, causing them to render as single characters rather than style commends.

  3. Use font.newFamily to define a font family where bold and italic all point to the same unstyled font, making the styles (and those characters) invisible.

It's easier than that.

Escape the characters by repeating them twice.

playdate.graphics.drawText("normal *bold* _italic_ asterisk ** underscore __", x, y)

Your real problem is sanitisation of user input.

1 Like

Another easy solution (and the reason #1 from @AdamsImmersive works when a font is specified) is to use playdate.graphics.font:drawText(text, x, y), which will not draw styled text (the logic being that you're drawing with that font, rather than using the font family system). Something like:

local font = playdate.graphics.font.new("Asheville-Sans-14-Light")
font:drawText("Hello_world*", 10, 10)

@matt I believe you will need to use a double asterisk or underscore instead of the backslash to have that solution work as expected (at least in my test right now I'm getting an invalid escape sequence error when using backslash escapes). So:

playdate.graphics.drawText("normal *bold* _italic_ asterisk ** underscore __", x, y)
2 Likes

Thanks Dan. I was going from memory in the middle of having flu.

1 Like