How to force browser to break text where possible?
I need to determine minimum width adequate for displaying a possibly
wrapped dynamic HTML string. Without word-wrapping this is simple: create
a span, set its innerHTML and read offsetWidth. However, I'm not sure how
to force linebreaks... Easiest incomplete approach I see is to replace all
spaces by <br/>, but lines can be wrapped not only on spaces but also e.g.
on hyphens.
So, basically, I want a browser to lay out sth. like
Max.
word-
wrapped
string
<----->
somewhere off-screen to measure width of the longest contained word. Is
there a generic way to do that?
EDIT
I.e., for no line wraps:
function computeWidth (str) { // code to make it off-screen and caching
omitted
var span = document.createElement ('span');
document.body.appendChild (span);
span.innerHTML = str;
return span.offsetWidth;
}
How would I write a similar function which forces line breaks on str,
presumably playing with span.style?
No comments:
Post a Comment