首页 > 解决方案 > 修复 MacOS 上 SVG 的 ios 与 Chrome 渲染之间的差异

问题描述

我想制作一个根据屏幕宽度增长/缩小的动态缩放标题。我还想让它不应用自动换行并将标题分成 2 行或更多行。我正在使用 SVG 技巧来完成这项工作。

考虑以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>StackOverflowExample</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
        type="text/css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
    <script src="https://code.iconify.design/1/1.0.6/iconify.min.js"></script>

</head>

<style>
text {
  font-family: 'Muli';
  font-weight: 600;
  fill: white;
}

svg {
  width: 100%;
}
a {
  color: white;
}
body {
  background-image: linear-gradient(180deg, rgba(35, 50, 64, 0.91) 61.95%, rgba(0, 0, 0, 0.51) 94.56%),
  /* height: 100vh; */
  width: 100vw;
  background-color: black;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  background-repeat: no-repeat;
  font-family: 'Muli';
  color: #ffffff;

  min-height: 100vh;
  display: flex;
  align-items: center;
}
</style>

<body class="text-center">
    <div class="container">

        <div class="row">
            <div class="col-12">
                <svg viewBox="0 0 167 20">
                    <text x="0" y="15">This is a Test Of This T</text>
                </svg>
            </div>

        </div>

    </div>
</body>
</html>

我让它在 Chrome 中完美运行,但我很惊讶在 ios 上,最后一个字母会被截断。请看截图:

IOS: 在此处输入图像描述

铬合金: 在此处输入图像描述

关于如何在两个操作系统/浏览器上使它看起来正确的任何想法?应该说“这是对This T的测试”。如果你看不到,无论我把它放在什么屏幕分辨率上(比 ios 更小或更大),Chrome 都会正确调整它的大小。在 ios 上,它总是将其切断。

如果我更改视图框参数,我可以在 1 个浏览器或其他浏览器上获得此信息,但我找不到任何适用于两者的设置。

标签: htmlcsstwitter-bootstrapdynamic-text

解决方案


得到它的工作......以备将来参考,以防其他人遇到这个......

添加textLength="167px"where 167 is the same as your viewbox size 解决了这个问题。显然,ios 和 MacOS 以不同方式呈现两个系统之间的间距,从而导致了该问题。定义一个 textLength 会强制它更均匀地呈现,从而消除该问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>StackOverflowExample</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
        type="text/css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
        integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
    <script src="https://code.iconify.design/1/1.0.6/iconify.min.js"></script>

</head>

<style>
text {
  font-family: 'Muli';
  font-weight: 600;
  fill: white;
}

svg {
  width: 100%;
}
a {
  color: white;
}
body {
  background-image: linear-gradient(180deg, rgba(35, 50, 64, 0.91) 61.95%, rgba(0, 0, 0, 0.51) 94.56%),
  /* height: 100vh; */
  width: 100vw;
  background-color: black;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  background-repeat: no-repeat;
  font-family: 'Muli';
  color: #ffffff;

  min-height: 100vh;
  display: flex;
  align-items: center;
}
</style>

<body class="text-center">
    <div class="container">

        <div class="row">
            <div class="col-12">
                <svg viewBox="0 0 167 20">
                    <text x="0" y="15" textLength="167px">This is a Test Of This T</text>
                </svg>
            </div>

        </div>

    </div>
</body>
</html>


推荐阅读