Is there any way for me to detect the texts that are cut off in a TextLabel?

I’m trying to make a fully functional readable book, which means I will have to work with very long texts, but I’m stuck as to how I’m going to fit all of that text on a frame, so my plan is to somehow cut the texts into multiple pieces via script and store them in a table.

I know how to detect that the text doesn’t fit thanks to the TextFits property, but then how do I split up the string from the last letter that did fit in? I’ve tried looping through the letters and adding them to a temporary TextLabel instance until TextFits becomes false, but that turned out to be a disaster :sweat_smile: and I can see why that wouldn’t be optimal

Basically, there’s more text to be displayed after that, it’s just limited to one side of the book because I use TextWrapped, I want to get the rest of the text that didn’t fit in and display it on the other page

2 Likes

TextLabel.ContentText property may work.

From what I’ve tried TextLabel.ContentText just gives me what TextLabel.Text already does, it doesn’t seem to be very useful, I heard it’s only useful if you use rich text

1 Like

Does your text scaled or fixed size?
If it’s fixed size, then you can get text bounds, with TextService, giving inf height and limited width, and then approximatelly split text by returned height.
Like if your page height is 250, and TextService returns height 600, then it means that text should cover 2 full pages and one by 40%

It’s a fixed size, I tried this:

local params = Instance.new("GetTextBoundsParams")
params.Text = bookContent.Value
params.Font = Font.new("rbxasset://fonts/families/AccanthisADFStd.json", Enum.FontWeight.Regular)
params.Size = 14
params.Width = viewportFrame.Page1.PageLeft.AbsoluteSize.X
local bounds = textService:GetTextBoundsAsync(params)
print(bounds)

It gave me an output of 210, 5096, so the height it gave is 5096, how do I use that info?
The size of the TextLabel I’m using is {0.534, 0},{0.834, 0}

  1. I must say that if player changes screen size, then you will need recalculate everything.
  2. You now need to get rough symbol count per page for text.
local Height = viewportFrame.Page1.PageLeft.AbsoluteSize.Y
local SymbolsPerPage = math.floor(string.len(bookContent.Value)*bounds.Y/Height)
local Pages = math.ceil(string.len(bookContent.Value)/SymbolsPerPage)
for i = 1, Pages, 1 do
    local PageText = string.sub(bookContent.Value, (i-1)*SymbolsPerPage+1, i*SymbolsPerPage)
    --Apply your text to page here.
end

Although, this won’t be ideal, because you are using not monowidth font, and such method may cut off 1-2 words.

Doesn’t seem to work,

local params = Instance.new("GetTextBoundsParams")
local width = viewportFrame.Page1.PageLeft.AbsoluteSize.X
local height = viewportFrame.Page1.PageLeft.AbsoluteSize.Y

params.Text = bookContent.Value
params.Font = Font.new("rbxasset://fonts/families/AccanthisADFStd.json", Enum.FontWeight.Regular)
params.Size = 14
params.Width = width
local bounds = textService:GetTextBoundsAsync(params)

local SymbolsPerPage = math.floor(string.len(bookContent.Value)*bounds.Y/height)
local Pages = math.ceil(string.len(bookContent.Value)/SymbolsPerPage)
for i = 1, Pages, 1 do
	local PageText = string.sub(bookContent.Value, (i-1)*SymbolsPerPage+1, i*SymbolsPerPage)
	viewportFrame.Page1.PageRight.Text = PageText
end

When I tried printing PageText it just gave a copy of the original text
Capture