Sometimes need to create … within large text in a tableview.
This function is very useful to me.
I hope it’s useful for you too.
Check out:
Sometimes need to create … within large text in a tableview.
This function is very useful to me.
I hope it’s useful for you too.
Check out:
Hi. Is there anyway to clip the text by width and add the optional suffix on a multi line text? Thank you in advance.
I’m not sure what you mean by adding a suffix. There is no way to clip without using a mask, but this won’t let stop a bit short and put ellipsis on. You just have to test the string length against what you think the cut point will be and append our own …
Rob
Thank you very much Rob. I guess I can do something like:
local txt = "hello world"; if (string.len(txt) \> 4) then txt = string.sub(1, 4); end display.newText(txt);
Unfortunately it’s really hard to determine how many characters fit inside a pixel width of 500. I’m trying to do something like phpedinei did in his function:
function display.clipTextByWidth(text, maxWidth, sufix) if text.width \<= maxWidth then return; end while text.width \> maxWidth do text.text = text.text:sub(1, -2); end text.text = text.text .. suffix end local eventTitle = display.newText("This is a long event title that I want to clip.", 0, 0, native.systemFontBold, 72); display.clipTextByWidth(eventTitle, 500, "...");
The code above, unfortunately does not work with multi-lines, and adding a width for the newText, over-rides the ability to clip the text and add the suffix.
I usually use the font height = average character width. While this isn’t true, an M or W is wider than an I, so you would think that perhaps font height / 2 or maybe font height / 3 * 2 (2/3 of the height) would be a better average, but remember there is usually a space character after each letter so height = character width works pretty close for a lot of fonts. Some narrow or extra wide fonts will be problematic.
Since Corona refreshes quickly, you should be able to try a couple of values until you find out which is closest to ideal. And even if you look at most apps, the ellipsis never line up exactly.
Rob
By the way, I came across this feed back / feature request while cleaning up the feature requests today:
It has one vote since Oct 2013. I was about to decline this due to a lack of interest, but if you guys go vote on it, I’ll leave it for now, but give there are only two of you in this and if you used your max votes, it would get it to 7, which isn’t nearly enough to implement this due to the amount of work required. But if you think this is important, and you can get it voted up, I can take it to the engineers, but it’s going to need significantly more interest.
Rob
Cool. Thank you very much Rob.
Thanks for sharing phpedinei, I find that function very useful. I realized that the resulting size is slightly wider than it should be, since the function was originally adding the suffix after checking for width. This small change should take care of it:
function display.clipTextByWidth(text, maxWidth, sufix) local subSize = 2 + string.len(sufix) if text.width \<= maxWidth then return; end while text.width \> maxWidth do text.text = text.text:sub(1, -subSize) .. sufix end end
Hi. Is there anyway to clip the text by width and add the optional suffix on a multi line text? Thank you in advance.
I’m not sure what you mean by adding a suffix. There is no way to clip without using a mask, but this won’t let stop a bit short and put ellipsis on. You just have to test the string length against what you think the cut point will be and append our own …
Rob
Thank you very much Rob. I guess I can do something like:
local txt = "hello world"; if (string.len(txt) \> 4) then txt = string.sub(1, 4); end display.newText(txt);
Unfortunately it’s really hard to determine how many characters fit inside a pixel width of 500. I’m trying to do something like phpedinei did in his function:
function display.clipTextByWidth(text, maxWidth, sufix) if text.width \<= maxWidth then return; end while text.width \> maxWidth do text.text = text.text:sub(1, -2); end text.text = text.text .. suffix end local eventTitle = display.newText("This is a long event title that I want to clip.", 0, 0, native.systemFontBold, 72); display.clipTextByWidth(eventTitle, 500, "...");
The code above, unfortunately does not work with multi-lines, and adding a width for the newText, over-rides the ability to clip the text and add the suffix.
I usually use the font height = average character width. While this isn’t true, an M or W is wider than an I, so you would think that perhaps font height / 2 or maybe font height / 3 * 2 (2/3 of the height) would be a better average, but remember there is usually a space character after each letter so height = character width works pretty close for a lot of fonts. Some narrow or extra wide fonts will be problematic.
Since Corona refreshes quickly, you should be able to try a couple of values until you find out which is closest to ideal. And even if you look at most apps, the ellipsis never line up exactly.
Rob
By the way, I came across this feed back / feature request while cleaning up the feature requests today:
It has one vote since Oct 2013. I was about to decline this due to a lack of interest, but if you guys go vote on it, I’ll leave it for now, but give there are only two of you in this and if you used your max votes, it would get it to 7, which isn’t nearly enough to implement this due to the amount of work required. But if you think this is important, and you can get it voted up, I can take it to the engineers, but it’s going to need significantly more interest.
Rob
Cool. Thank you very much Rob.
Thanks for sharing phpedinei, I find that function very useful. I realized that the resulting size is slightly wider than it should be, since the function was originally adding the suffix after checking for width. This small change should take care of it:
function display.clipTextByWidth(text, maxWidth, sufix) local subSize = 2 + string.len(sufix) if text.width \<= maxWidth then return; end while text.width \> maxWidth do text.text = text.text:sub(1, -subSize) .. sufix end end