How to either determine SVG text box width, or force line breaks after 'x' characters?

后端 未结 5 1397
猫巷女王i
猫巷女王i 2020-12-04 15:00

I\'m creating an SVG text box using the Raphael library, and filling it with a dynamic string which is extracted from an XML document.

Sometimes, this string is long

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 15:05

    Well, i solved it tweaking it a little bit

    var words = server.split( " " );
    var length = words.length;
    var temp_text = "";
    
    for( var i = 0; i < length; i++ ) {
        temp_text = temp_text + ' ' + words[i];
        t.attr( "text", temp_text );
    
        if( t.getBBox().width > width ) {
            temp_text = temp_text.replace(/( *)(\w+)$/, "\n$2");
        }
    }
    
    t.attr( "text", temp_text.trim() );
    

提交回复
热议问题