How to replace long text by dots (...)

Hi,

I have a listview with some very long titles in.
I want to break the text at the end of the screen and replace it by 3 dots

Ex: "Creating iPhon apps is very fun and pl..."

Any idea how to do this?

http://lua-users.org/wiki/StringLibraryTutorial

Loop through your string adding one letter at a time (plus ...) to a newText until your width is too long. Then use the one before that.

If it's a monospace font you can work out this length in advance

using sub seems more straightforward?

longstring = "long text here"
shortstring = string.sub(longstring, 0,6)
shortstringwithdots = shortstring .. "..."

don't forget though if you're not using fixed width fonts then

WWWWWWWWWW

is longer than

iiiiiiiiii

Thank you all for your replies,

I have created a working function to do this.
Here's the code if someone is also looking for this:

function writeTitleLabel(g,x,y,txt, maxwidth)
local title = display.newText( txt, 0, 0, native.systemFontBold, 13*2 )
title:setTextColor(0,0,0)
g:insert(title)
title.y = y
title.xScale = 0.5
title.yScale = 0.5

if maxwidth and title.width > maxwidth then
local aantal = 1
while true do
title.text = string.sub(txt, 0,aantal) .. "..."
if(title.contentWidth >= maxwidth) then
break
elseif(aantal >= string.len(txt)) then
break
end
aantal = aantal + 1
end
end
title.x = title.contentWidth * 0.5 + x
end

I'm trying this but it is not working and I don't know why? Any idea for the begginners?

1
2
3
4
5
6
7
8
9
10
11
12
local movieName = display.newText("Onde os fracos não tem vez", 50, 100, native.systemFontBold, 14)
local addPoints = (movieName.contentWidth > 100)
 
-- remove last char while it is big
while movieName.contentWidth > 100 do
        movieName.text = string.sub(movieName.text, 1, string.len(movieName.text)-1)
end
 
-- add the points
if addPoints then
        movieName.text = movieName.text .. " ..."
end
views:1637 update:2011/10/3 8:06:12
corona forums © 2003-2011