SVG 文本(text)

属性介绍

text-anchor

MDN开发文档 https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/text-anchor

  • start
    The rendered characters are aligned such that the start of the text string is at the initial current text position. For Latin in its usual orientation this is equivalent to left alignment. For scripts that are inherently right to left such as Hebrew and Arabic, this is equivalent to right alignment. For Asian text with a vertical primary text direction, this is comparable to top alignment.
  • middle
    The rendered characters are aligned such that the middle of the text string is at the current text position. (For text on a path, conceptually the text string is first laid out in a straight line. The midpoint between the start of the text string and the end of the text string is determined. Then, the text string is mapped onto the path with this midpoint placed at the current text position.)
  • end
    The rendered characters are aligned such that the end of the text string is at the initial current text position.For Latin in its usual orientation this is equivalent to right alignment. For scripts that are inherently right to left such as Hebrew and Arabic, this is equivalent to left alignment.

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="120" height="120" viewBox="0 0 120 120">
    
    <path d="M60,15 V 110 M30,40 H 90 M30,75 H 90 M30,110 H 90" stroke="grey" /> 
    
    <text text-anchor="start" x="60" y="40">Atext>
    <text text-anchor="middle" x="60" y="75">Atext>
    <text text-anchor="end" x="60" y="110">Atext>
    
    <circle cx="60" cy="40" r="3" fill="red" />
    <circle cx="60" cy="75" r="3" fill="red" />
    <circle cx="60" cy="110" r="3" fill="red" />

    <style>
    [CDATA[    text{        font: bold 36px Verdana, Helvetica, Arial, sans-serif;    }    ]]>
    style>
svg>

SVG 文本(text)_第1张图片

textlength 和 lengthAdjust

MDN开发文档 https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/lengthAdjust

When an SVG or element has a specific length set using the textLength attribute, the lengthAdjust attribute controls how the text is stretched or compressed into that length.

The two possible values for the attribute are spacing and spacingAndGlyphs. Using spacing (the default), the letter forms are preserved, but the space between them grows or shrinks. Using spacingAndGlyphs, the entire text element is stretched in the direction of the text.

"http://www.w3.org/2000/svg" width="450" height="200">
    "sans-serif">
        <text x="0" y="20">Stretched using spacing only.text>
        <text x="0" y="50">Stretched using spacing and glyphs.text>
        <text x="0" y="80" textLength="400">Stretched using default.text>
        <text x="0" y="110" textLength="400" lengthAdjust="spacing">Stretched using spacing only.text>
        <text x="0" y="140" textLength="400" lengthAdjust="spacingAndGlyphs">Stretched using spacing and glyphs.text>
        <text x="0" y="170" textLength="100" lengthAdjust="spacing">Shrunk using spacing only.text>
        <text x="0" y="200" textLength="100" lengthAdjust="spacingAndGlyphs">Shrunk using spacing and glyphs.text>
    

SVG 文本(text)_第2张图片

transform

MDN开发文档 https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform

<html>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg" width="250" height="500">
            <text x="0" y="10" dy="10" fill="red" transform="translate(20, 0)">I love SVGtext>
            <text x="0" y="10" dy="60" fill="green" transform="scale(1.5, 1.5)">I love SVGtext>
            <text x="0" y="10" dy="110" fill="blue" transform="rotate(30 30,200)">I love SVGtext>
            <text x="0" y="10" dy="160" fill="orange" transform="skewX(30)">I love SVGtext>
            <text x="0" y="10" dy="210" fill="purple" transform="skewY(30)">I love SVGtext>
        svg>
        <svg xmlns="http://www.w3.org/2000/svg" width="250" height="500">
            <text x="0" y="10" dy="10" fill="red" transform="matrix(1,0,0,1,20,0)">I love SVGtext>
            <text x="0" y="10" dy="60" fill="green" transform="matrix(1.5,0,0,1.5,0,0)">I love SVGtext>
            <text x="0" y="10" dy="110" fill="blue" transform="matrix(0.866,0.5,-0.5,0.866,0,0)">I love SVGtext>
            <text x="0" y="10" dy="160" fill="orange" transform="matrix(1,0,0.577,1,0,0)">I love SVGtext>
            <text x="0" y="10" dy="210" fill="purple" transform="matrix(1,0.577,0,1,0,0)">I love SVGtext>
        svg>
    body>
html>

SVG 文本(text)_第3张图片

用来细分文本内容并设置不同风格

<html>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
            <text x="0" y="30" fill="red">I love SVGtext>
            <text x="0" y="60" fill="black">
                <tspan transform="translate(2,1.5)">Itspan>
                <tspan y="70" dx="10" rotate="30"> love tspan>
                <tspan fill="blue">SVGtspan>
            text>
            <text x="0" y="90" fill="green" transform="translate(2,1.5) scale(2,1.5)">I love SVGtext>
        svg>
    body>
html>

注意:在标签中,transform属性并不生效。但是tspan居然支持scale属性,该属性在其它标签中均不被支持。

SVG 文本(text)_第4张图片

用来设置文本沿着路径方向排列

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <path id="path1" d="M75,20 a1,1 0 0,0 100,0"/>
    defs>
    <text x="10" y="100" style="fill:red;">
        <textPath xlink:href="#path1">I love SVG I love SVGtextPath>
    text>
svg>

这里写图片描述

将文本作为链接 的元素

xlink定义了一套标准的在 XML 文档中创建超级链接的方法

<html>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
            <a xlink:href="http://www.w3cschool.cc/svg/" target="_blank">
                <text x="0" y="15" fill="red">I love SVGtext>
            a>
        svg>
    body>
html>

这里写图片描述

你可能感兴趣的:(——,SVG)