I know that it's possible to split a string based on its length by number of characters. But how can I split an html string based on pixels without cutting words off?
For example :
myString = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
splitThroughPixel(myString, 100) // Shall return something like ["Lorem Ipsum has", "been the industry's", "dummy text", "since the", "1500s"] (not the true splitting, just to give an idea)
splitThroughPixel(myString, 100)
shall split myString
into string pieces of 100px
max each (without cutting words).
How can I achieve that ?
I'm already able to get the full pixel length of a string using this javascript method (if it can ever help) :
function getWidth(pText, pFontSize, pStyle) {
var lDiv = document.createElement('div');
document.body.appendChild(lDiv);
if (pStyle != null) {
lDiv.style = pStyle;
}
lDiv.style.fontSize = "" + pFontSize + "px";
lDiv.style.position = "absolute";
lDiv.style.left = -1000;
lDiv.style.top = -1000;
lDiv.innerHTML = pText;
document.body.removeChild(lDiv);
lDiv = null;
return lDiv.clientWidth;
}
For example : getWidth(myString )
return 510
(which is the number of pixel on the screen of the string myString
)
Thanks for taking time for helping me.