Trimming text to a given pixel width in SVG

后端 未结 8 1791
感动是毒
感动是毒 2021-02-01 18:14

I\'m drawing text labels in SVG. I have a fixed width available (say 200px). When the text is too long, how can I trim it ?

The ideal solution would also add ellipsis (.

8条回答
  •  [愿得一人]
    2021-02-01 18:41

    The linearGradient element can be used to produce a pure SVG solution. This example fades out the truncated text (no ellipsis):

    
      
        
          
          
        
        
        
      
    
      0123456789
      0123456789
      
    

    (I had to use linear gradients to solve this because the SVG renderer I was using does not support the textPath solution.)

提交回复
热议问题