开发者

jruby trim string based on length with ellipses

This is a pretty specific problem, but I'm wondering if anyone would have a brilliant solution. I'开发者_C百科m trying to fit a string with a fixed font size in a box of arbitrary size, such that, if the whole string doesn't fit, I want to trim it and ad an ellipses (...).

So if my text is "the netherlands" I want to determine what portion of that fits in my box of arbitrary size to look something like "the nether..."

I'm using jruby, so java or ruby is fine. I can't come up with a fantastic solution, other than trimming/testing char-by-char to see if the string fits. Since it's not a fixed width font, I can't just take each char as the same width, which would make this much easier.

Any thoughts, or tips that might point me in the right direction?


The character-by-character loop is probably just fine. But if you're doing billions of them and need to speed them up, you could binary-search for the right length instead of just iterating through the characters. Here's a generic binary_search method you can call on an Integer. Give it a <=> comparison block that returns -x 0 or +x, and it iteratively jumps by half as much each time until either the test returns 0 or the jump size gets too small.

class Integer
  def binary_search
    current = self
    nextjump = current / 2.0
    until (test = yield(current)) == 0 || nextjump < 0.5
      if test < 0
    current += nextjump
      else
        current -= nextjump
      end
      nextjump /= 2.0
    end
    current
  end
end

Then use it like this:

goal = whatever
a[0...(a.size.binary_search {|i| a[0...i].stringWidth - goal})]


An alternate approach would be to handle the truncation of the display in CSS!

<div style="border: 1px solid gray; width: 200px;">
<span style="float: right; color: gray">...</span>
<div style="width: 184px; overflow: hidden; white-space: nowrap">
This is a really long string that goes on and on and on and we need to do something about it eventually but for now it just isn't important so I don't want to think about it.
</div></div>

This gets you out of the thankless task of worrying about your users' browsers' font metrics (which you don't know, and to which your Java server's font metrics thus bear an unknown relationship).


take a look at java's FontMetrics class, and in particular the stringWidth method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜