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? [import]uid: 13464 topic_id: 7792 reply_id: 307792[/import]

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
[import]uid: 6645 topic_id: 7792 reply_id: 27671[/import]

using sub seems more straightforward?

longstring = “long text here”
shortstring = string.sub(longstring, 0,6)
shortstringwithdots = shortstring … “…”
[import]uid: 44968 topic_id: 7792 reply_id: 27784[/import]

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

WWWWWWWWWW

is longer than

iiiiiiiiii [import]uid: 6645 topic_id: 7792 reply_id: 27806[/import]

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 [import]uid: 13464 topic_id: 7792 reply_id: 27828[/import]

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

[code]
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
[/code] [import]uid: 6732 topic_id: 7792 reply_id: 33650[/import]