Reputation: 14427
I've got some user-entered text I'm displaying on the page that needs to be truncated nicely so as to fit into a fixed width. Rails has a nice helper built in for truncating strings, and specifying the omission text like so:
truncate(post.title, :length=>50, :omission=>link_to("...",post))
or I can css it like so but neither of these are quite satisfactory. I want the ellipsis to link to the original/full text in the case that some text is truncated, and I want the truncation to work with respect to the actual space taken up by the characters at time of rendering (without using a fixed-width font), not with respect to a character count.
so. is there a fix to the css version to use an arbitrary html string for the truncation text? or is there a way for rails to tell how much space the string is gonna take up when it is truncating? It seems to be a problem that people have since there's a built-in Rails helper for truncating text, but are these people all using fixed-width fonts?
Upvotes: 2
Views: 796
Reputation: 22721
There is no way to know how wide the rendering will be until it is rendered. It will be different with various browsers / devices / font availability / settings.
Your only solution will be to calculate the width via javascript and then modify the markup, generate your ellipses link, etc.
Many relevant thingies in several answers here: Calculate text width with JavaScript
update this particular answer even adds a … for you: https://stackoverflow.com/a/3140086/168143
Upvotes: 2