FontString:GetStringWidth

From Warcraft Wiki
Jump to navigation Jump to search

FontString provides three methods to determine the width of its text independently from the value returned by Region:GetWidth:

  • GetStringWidth() ignores word wrap but includes truncation
  • GetUnboundedStringWidth() ignores word wrap and truncation
  • GetWrappedWidth() includes word wrap and truncation
         stringWidth = fontString:GetStringWidth()
unboundedStringWidth = fontString:GetUnboundedStringWidth()
        wrappedWidth = fontString:GetWrappedWidth()

Returns

stringWidth
Number - The minimum width necessary to contain the currently-displayed, possibly-truncated, text on a single line if WordWrap became forbidden; e.g. the width if word-wrapped lines were strung end-to-end
unboundedStringWidth
Number - The minimum width necessary to contain the entire text without truncation if WordWrap became forbidden; e.g. the width if ignoring any fixed anchor points or width
wrappedWidth
Number - The minimum width necessary to contain the currently-displayed text without changing its shape; e.g. the width of the widest line after applying word wrap and truncation

Notes

  • By default, a FontString expands and shrinks to fit its text.
    • Region:GetWidth() returns the width that the FontString has dynamically adopted
    • width = wrappedWidth = stringWidth = unboundedStringWidth
  • Conversly, a FontString may have explicitly defined width using Region:SetPoint() or Region:SetWidth; shorter texts fit inside and longer ones must wrap or truncate.
    • Region:GetWidth() will return the explicitly defined width
    • wrappedWidth <= width because GetWrappedWidth() only ever describes a single line (either the whole text or the biggest line that could fit)
    • However, GetStringWidth() and GetUnboundedStringWidth() reflect wrapping and truncation:
      • If the text fits on one line, stringWidth = unboundedStringWidth <= width
      • If the text fits on multiple lines, width < stringWidth = unboundedWidth
      • If the text cannot wrap and must truncate, stringWidth <= width < unboundedWidth
      • If the text wraps but truncates after multiple lines, width < stringWidth < unboundedWidth
  • Notwithstanding, a text with manual line breaks (such as the |n escape sequence) could have a smaller width
    • Manually broken lines are not strung end-to-end when calculating GetStringWidth() or GetUnboundedStringWidth()
    • In other words, a manually broken text does not need its FontString to be as wide (but it does need more height)

See also