After researching similar posts on Stack Overflow, I have not found a solution to my specific query. Imagine having some text enclosed within a div with an arbitrary width. Is there a way to programmatically capture and manipulate individual lines of this text? For instance, wrapping each line in its own span tag for customization?
I managed to achieve this using a monospace font by creating one span per line, ensuring each span had the same number of characters (with extra code to prevent word cut-off). However, I aim to replicate this with non-monospaced fonts, which pose a challenge due to varying character spacing.
var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
container = $('<div>');
container.width('100px').html(str).appendTo('body');
The resulting output can be viewed live here. My queries are as follows:
Do newline characters automatically get inserted for line breaks?
Are there alternative mechanisms or DOM properties I can utilize to manipulate individual lines within a div?
Is there another unexplored method to maintain the natural appearance of flowing non-monospaced text while retaining access to text on a line-by-line basis? While successful with monospaced text, my approach relied on uniform horizontal spacing.