Disable styling text in playdate.graphics.drawTextInRect()

In my video player app, I’m using playdate.graphics.drawTextInRect() to display the name of the video files (user content). But these can contain underscores, which automatically converts to italics. I know we can escape styling by doubling the style character. And I know we can also use playdate.graphics.font:drawText(), but I miss all the goodness from drawText functions there (truncation, drawInRect, …).

I feel like the SDK should provide a boolean option to the playdate.graphics.drawText to simply disable text styling.

From the drawTextInRect docs:

font , if provided, will cause the text to be drawn unstyled using font:drawText() rather than playdate.graphics.drawText() using the currently-set system fonts.

But then you can't use things like drawInRect, truncation and such, which was the point of my request. Or am I missing something?

1 Like

You can’t? I might be looking at the wrong function but it has it all:

playdate.graphics.drawTextInRect(
  text, 
  x, y, width, height, 
  [leadingAdjustment, 
  [truncationString, 
  [alignment, 
  [font]]]]
)

I think you're confusing the two functions. To summarize:

  • playdate.graphics.drawTextInRect(): allows truncation, wrapping, but NOT disable styling
  • playdate.graphics.font:drawText() : disables styling, but DOES NOT HAVE truncation, wrapping

Or am I missing something?

Full documentation for playdate.graphics.drawTextInRect(text, x, y, width, height, [leadingAdjustment, [truncationString, [alignment, [font]]]]), with some added emphasis:

Draws the text using the current font and font advance into the rect defined by (x, y, width, height) (or rect).

If truncationString is provided and the text cannot fit in the rect, truncationString will be appended to the last line.

alignment, if provided, should be one of one of kTextAlignment.left, kTextAlignment.center, kTextAlignment.right. Pass nil for leadingAdjustment and truncationString if those parameters are not required.

font, if provided, will cause the text to be drawn unstyled using font:drawText() rather than playdate.graphics.drawText() using the currently-set system fonts.

For text formatting options, see drawText()

Returns width, height, textWasTruncated

width and height indicate the size in pixels of the drawn text. These values may be smaller than the width and height specified when calling the function.

textWasTruncated indicates if the text was truncated to fit within the specified rect.

Oh my, you're right. Sorry for the confusion and my misunderstanding. I totally skipped this parameter to the function and it does exactly what I was looking for. Thank you for your help and your perseverance!

1 Like

No worries, good luck with your app!!