3

I have a fixed size div which I need to fill with text. I need to only fill it with as much text will fit into it, and I need to know the offset in the string where it cut off.

Right now I am doing it by saving the div's current height then setting the div's height to auto, taking increasingly long substrings of the string and putting them into the div, and stopping when the div's height gets bigger than the original height. This works, but it's pretty inefficient.

Is there any better way to do this? And by better I mean faster.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • I am working on it right now. But I have a question. What are the width/height of your div to put the text in? – Will Sep 04 '11 at 01:43
  • @William it shouldn't matter, the function to do this should be able to take any fixed-size div and fill it (my current one does this). I don't need you to write the whole function for me though, I can do that, I just need to know the method of determining how much text to put in. Thanks for working on it. – Seth Carnegie Sep 04 '11 at 01:45

2 Answers2

5

Well, I came up with this: http://jsfiddle.net/r5Njr/. That should fit the text into the div. If you want to be fancy, you can change the last 2 or 3 characters to a ...

Will
  • 19,661
  • 7
  • 47
  • 48
  • This is exactly what I'm already doing, and I'm doing 50 chars at a time, not 1, and it's still too slow. Thanks though. – Seth Carnegie Sep 04 '11 at 02:08
  • Alright... slight modification. Sounds like you have a lot of text. So, this version cuts it down (by calculation, not removing characters) until it is one line above the desired line. Then it cuts if off by characters. [http://jsfiddle.net/r5Njr/2/](http://jsfiddle.net/r5Njr/2/) – Will Sep 04 '11 at 02:56
  • that seems better, but it always comes up with the same text no matter how wide the div is. For example, look at this: http://jsfiddle.net/r5Njr/5/ – Seth Carnegie Sep 04 '11 at 03:03
  • That was because it made the calculations regardless of whether the text fit or not. Now it tests before making the calculations. [http://jsfiddle.net/r5Njr/6/](http://jsfiddle.net/r5Njr/6/) – Will Sep 04 '11 at 03:14
  • I can't test it now, but from what I can see, it looks like this will work nicely. Thanks very much! – Seth Carnegie Sep 04 '11 at 03:27
0

Maybe I don't get the question correctly but why not use substr and fetch an exact characters number?

jribeiro
  • 3,387
  • 8
  • 43
  • 70
  • How do I know what the exact characters number is? I only have a div (which is a fixed size) and a string. I need to know how much text from that string to put into the div so that it will exactly fit it. – Seth Carnegie Sep 04 '11 at 01:30
  • well it would always be an aproximation 'cause different chars require a bit more chars. But i would do a trial an error 'til i find an aproximate number of chars. Then just use substr to get that fixed number all the time. If needed check the browser and fetch a different number of chars for different browsers. It's always a aproximation but is fast and should be enough i think – jribeiro Sep 04 '11 at 01:37
  • An approximation isn't good enough, I need it to be exact. Also due to variable-width characters, one text won't even be close to another text. – Seth Carnegie Sep 04 '11 at 01:39
  • That's helpful but doesn't solve my problem. I can't modify the text I'm working with. – Seth Carnegie Sep 04 '11 at 01:48