For display.newText(), is it possible to disable multiline & still cropped within w/h ?

I am trying to use display.newText() to print something within a range (width/height)

but I found that… for example, I want to print 

“James Cameron”

And the width is enough to print “James Came”, however the result is

“James”

that’s all, “Cameron” is printed in the next line, but it’s cropped anyway because I can only show one line of text by limiting its height.

So is it possible or any work around to show “James Came” instead?

Set height to 0 in parameters of multiline text.
It will automatically resize.

Also try to use smaller font and check if it really fits in one line.

I can’t change font size, because the content of the text is not fixed.

Sometimes the text is long & sometimes it’s short.

And I am printing them in a table kind of UI, so I want to maintain the same font size.

Setting height to 0 will only print all of the text in multiline, isn’t it? 

I am saying to change font size for your example text to check if it really ‘should’ be in one line.

I am printing contexts for a ladder board, so one row, one line.

Basically you want to trim your text if it exceeds a width…
Shouldn’t be hard to write a custom one.

Here’s a simple one which I use 
 

local function trim(text,options)    local txt = text.text     local trimW = options.width     local trimSuffix = options.suffix or ""     local align = options.align or "left"    local bounds = text.contentBounds    local xMin,xMax = bounds.xMin,bounds.xMax    local yMin,yMax = bounds.yMin,bounds.yMax    local w,h = xMax - xMin, yMax - yMin      local l = text.x - w/2     local x = text.x     local r = text.x + w/2      local c = 0     local trimmed    while text.width \> trimW do        trimmed = true        txt = txt:sub(1,-2)        text.text = txt         if align == "left" then             text.x = l + text.width/2        elseif align == "center" then             text.x = x        elseif align == "right" then             text.x = r - text.width/2        end              c = c + 1        if c \> 100 then break end      end     if trimmed then          text.text = text.text .. trimSuffix    end end 

A bit clumsy, but gets the job done.

Takes 2 parameters : the first is the text object
The second is a set of options. 
Options takes 3 values

  1. width - the width above which the text should be trimmed(this is compulsory)
  2. align  - how to align the trimmed text : “left”,“right”,“center” (optional)
  3. suffix - if you want to append some text after trimming (for  eg. a few dots “…” )  (optional)

eg;   
trim(text, {width = 100})

@Satheesh

Aha, you remind me that I can change text in run-time without removing the text object (& recreate again).

I will take a look at your code & see how it can be applied to my situation.

Thanks a lot for your reply.

Set height to 0 in parameters of multiline text.
It will automatically resize.

Also try to use smaller font and check if it really fits in one line.

I can’t change font size, because the content of the text is not fixed.

Sometimes the text is long & sometimes it’s short.

And I am printing them in a table kind of UI, so I want to maintain the same font size.

Setting height to 0 will only print all of the text in multiline, isn’t it? 

I am saying to change font size for your example text to check if it really ‘should’ be in one line.

I am printing contexts for a ladder board, so one row, one line.

Basically you want to trim your text if it exceeds a width…
Shouldn’t be hard to write a custom one.

Here’s a simple one which I use 
 

local function trim(text,options)    local txt = text.text     local trimW = options.width     local trimSuffix = options.suffix or ""     local align = options.align or "left"    local bounds = text.contentBounds    local xMin,xMax = bounds.xMin,bounds.xMax    local yMin,yMax = bounds.yMin,bounds.yMax    local w,h = xMax - xMin, yMax - yMin      local l = text.x - w/2     local x = text.x     local r = text.x + w/2      local c = 0     local trimmed    while text.width \> trimW do        trimmed = true        txt = txt:sub(1,-2)        text.text = txt         if align == "left" then             text.x = l + text.width/2        elseif align == "center" then             text.x = x        elseif align == "right" then             text.x = r - text.width/2        end              c = c + 1        if c \> 100 then break end      end     if trimmed then          text.text = text.text .. trimSuffix    end end 

A bit clumsy, but gets the job done.

Takes 2 parameters : the first is the text object
The second is a set of options. 
Options takes 3 values

  1. width - the width above which the text should be trimmed(this is compulsory)
  2. align  - how to align the trimmed text : “left”,“right”,“center” (optional)
  3. suffix - if you want to append some text after trimming (for  eg. a few dots “…” )  (optional)

eg;   
trim(text, {width = 100})

@Satheesh

Aha, you remind me that I can change text in run-time without removing the text object (& recreate again).

I will take a look at your code & see how it can be applied to my situation.

Thanks a lot for your reply.