Ah, I think failed to communicate the issue I’m talking about.
There is no misalignment when overlaying using a full line as in your repro place, since the X position of your overlay is 0 and 0 stays the same regardless of DPI. This method works (as far as my testing has found, at least) and covers some of my use cases.
The issue is when attempting to cover a specific word, not an entire line. Since the X position of the word might not be 0 anymore and the HiDPI means that X position is no longer just math.ceil(TextSize/2) * WordDepth
the overlay label might not accurately cover the word.
In my earlier reply, I mentioned using a workaround for this that is similar to how we solved the Y axis.
-- Find the longest line
local LongestLine = 1
local Lines = string.split(Text, "\n")
for _, line in ipairs(Lines) do
LongestLine = math.max(LongestLine, #line)
end
-- Compute the width of a char, assuming monospace font
local TextSizeX = TextObject.TextBounds.X / LongestLine
-- Position the overlay
-- CurrentDepth = x pos, CurrentLine = y pos
local Position = UDim2.fromScale(
(CurrentDepth * TextSizeX) / AbsoluteSizeX,
(LineHeight * (CurrentLine - 1)) / AbsoluteSizeY
)
Rather than get an offset pixel value for TextSizeX like on non HiDPI, here we use a Scale value that’s computed from TextBounds.
The problem with this is that it only works on a monospace font- it assumes that each character is evenly spaced so the width of a character would be totalWidth/numChars
.
Here’s this method working on Code, a monospace font:
Here we see it fails on a proportional font, Gotham:
Note that because our method assumes monospace, it will fail on proportional regardless of DPI.
I have updated the original place with this new version: (which also includes cleaner code and only one script, making it easier to work with)