SVG 入门教程(六) 文本

添加文本<nobr>第 1 页(共3 页)</nobr>


SVG 的强大能力之一是它可以将文本控制到标准 HTML 页面不可能有的程度,而无须求助图像或其它插件(后者会带来可访问性挑战)。任何可以在形状或路径上执行的操作(如绘制或滤镜)都可以在文本上执行。

一个不足之处是 SVG 不执行自动换行。如果文本比允许空间长,则简单地将它切断。多数情况下,创建多行文本需要多个文本元素。

可以使用 tspan 元素将文本元素分成几部分,允许每部分有各自的样式。在 text 元素中,空格的处理与 HTML 类似;换行和回车变成空格,而多个空格压缩成单个空格,如下面的早期示例所示:

xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg"  
  4.                       xmlns:xlink="http://www.w3.org/1999/xlink">  
  5.      
  6.   <desc>Textdesc>  
  7.   <defs>  
  8.   defs>  
  9.      
  10.   <g>  
  11.   
  12.      <text x="20" y="50" font-size="30">  
  13.         Colors can be specified   
  14.      text>  
  15.      <text x="20" y="100" font-size="30">by their   
  16.         <tspan fill="rgb(255,0,0)">Rtspan>  
  17.         <tspan fill="rgb(0,255,0)">Gtspan>  
  18.         <tspan fill="rgb(0,0,255)">Btspan>  
  19.      valuestext>  
  20.      <text x="20" y="150" font-size="30">  
  21.         or by keywords such as   
  22.      text>  
  23.      <text x="20" y="200" font-size="30">  
  24.         <tspan fill="lightsteelblue">lightsteelbluetspan>,   
  25.      text>  
  26.      <text x="20" y="250" font-size="30">  
  27.         <tspan fill="mediumseagreen">mediumseagreentspan>,   
  28.      text>  
  29.      <text x="20" y="300" font-size="30">and   
  30.         <tspan fill="darkorchid">darkorchidtspan>.   
  31.      text>  
  32.   
  33.   g>  
  34. svg>  
  35.   
  36.   

 


使用 CSS 属性<nobr>第 2 页(共3 页)</nobr>


实际上,所有的属性(对于所有元素,不仅是文本)都可以用级联样式表与一个元素关联,并且文本的所有 CSS 属性都在 SVG 图像中可用。

可以直接用样式属性设计元素的样式,或者引用样式表设计元素的样式。不应该解析样式表(因为它们偶尔包含会引起问题的字符),因此将它们置于 XML CDATA 节。

xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg">  
  4.      
  5.   <desc>Textdesc>  
  6.   <defs>  
  7.     <style type="text/css">  
  8.        
  9.       .abbreviation { text-decoration: underline; }  
  10.       ]]>                      
  11.     style>  
  12.   defs>  
  13.      
  14.   <g>  
  15.   
  16.      <text x="20" y="50" font-size="30">Colors can be specifiedtext>  
  17.      <text x="20" y="100" font-size="30">by their   
  18.         <tspan fill="rgb(255,0,0)" class="abbreviation">Rtspan>  
  19.         <tspan fill="rgb(0,255,0)" class="abbreviation">Gtspan>  
  20.         <tspan fill="rgb(0,0,255)" class="abbreviation">Btspan>  
  21.      valuestext>  
  22.      <text x="20" y="150" font-size="30">or by keywords such astext>  
  23.      <text x="20" y="200">  
  24.         <tspan style="fill: lightsteelblue; font-size:30">  
  25.            lightsteelblue   
  26.         tspan>,   
  27.      text>  
  28. . . .   
  29.   g>  
  30. svg>  
  31.   
  32.   

 

路径上的文字<nobr>第 3 页(共3 页)</nobr>


在纯 HTML 中不可能具有的一个 SVG 能力是将文本沿路径排列。要实现这一点,需创建一个链接到预定义的路径信息的 textPath 元素:

xml 代码
  1. xml version="1.0" standalone="no"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="300" xmlns="http://www.w3.org/2000/svg"  
  4.                       xmlns:xlink="http://www.w3.org/1999/xlink">  
  5.   <desc>Text on a pathdesc>  
  6.   <defs>  
  7.      <path id="wavyPath"    
  8.          d="M75,100 c25,-75 50,50 100,0 s50,-50 150,50"/>  
  9.   defs>  
  10.   <rect x="1" y="1" width="398" height="200"  
  11.         fill="none" stroke="blue" />  
  12.   
  13.   <text x="50" y="50" font-size="14">  
  14.      <textPath xlink:href="#wavyPath">  
  15.        Text travels along any path that you define for it.   
  16.      textPath>  
  17.   text>  
  18.   
  19. svg>  
  20.   
  21.   

 

 

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值