question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

letterSpacing (and kerning) making text go off-centred

See original GitHub issue

I’m trying to space out my text a bit because it looks a little crunched when tying to render it in a circular arc shape. However, it seems that any calculations regarding the origin point to start drawing text is computed without taking letterSpacing or kerning into account.

For example, here is my component:

import React from 'react';
import Svg, { Circle, Defs, G, Path, Text, TextPath } from 'react-native-svg';

const Medallion = props => {
  const upperArc  = 'M-40.97502384803651,-1.4308810757150652A41,41,0,0,1,40.97502384803651,-1.4308810757150694A41,41,0,0,0,-40.97502384803651,-1.4308810757150652Z'
  const upperPath = 'M-40.97502384803651,-1.4308810757150652A41,41,0,0,1,40.97502384803651,-1.4308810757150694';

  const lowerArc  = 'M-48.97015053189642,1.7100751104568743A49,49,0,0,0,48.97015037316634,1.7100796558864861A49,49,0,0,1,-48.97015053189642,1.7100751104568743Z';
  const lowerPath = 'M-48.97015053189642,1.7100751104568743A49,49,0,0,0,48.97015037316634,1.7100796558864861';

  return (
    <Svg width={114} height={114}>
      <Defs>
        <Path id="upperPath" d={upperPath} />
        <Path id="lowerPath" d={lowerPath} />
      </Defs>
      <Circle cx={57} cy={57} r={57} fill="blue" />
      <Text
        x={57}
        y={57 + 10}
        textAnchor="middle"
        fill="white"
        fontWeight="bold"
        fontSize={10}
        letterSpacing="2"
      >
        This is a test
      </Text>
      <G x={57} y={57}>
        <Path d={upperArc} stroke="pink" />
        <Path d={lowerArc} stroke="pink" />
        <Text fill="white" stroke="white" fontSize={12} textAnchor="middle">
          <TextPath href="#upperPath" startOffset="50%">
            No letterSpacing
          </TextPath>
        </Text>
        <Text
          fill="white"
          stroke="white"
          fontSize={12}
          textAnchor="middle"
          letterSpacing="2"
        >
          <TextPath href="#lowerPath" startOffset="50%">
            This is a test
          </TextPath>
        </Text>
      </G>
    </Svg>
  );
};

export default Medallion;

I draw upperArc and lowerArc paths so that you can see the path I’m using to draw my text. I’ve removed the inner arc (equal to outer arc) to actually draw the text so that I don’t get wrap around. This is the result:

letterspacing

You can see from the output that the text drawn with Text in the middle of the circle is off-centred as well as the text on the lower arc. Both of these have letterSpacing="2" (which, if I remove, would make the text centred again).

I’ve tried something similar with JS + D3 + SVG in a browser environment and it works as expected, staying centred. Any idea what is going on here and how I can work around it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:35

github_iconTop GitHub Comments

1reaction
oleksandr-dziubancommented, Mar 21, 2019

Thank you

1reaction
oleksandr-dziubancommented, Feb 11, 2019

@msand Checked, it works fine for my case! Text alignment works perfectly on iOS/Android, checked with <Text> + few <TSpan> inside with letterSpacing + textAnchor=“middle”. Please merge these changes to master and publish new npm package version. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

letterSpacing (and kerning) making text go off-centred #570
I'm trying to space out my text a bit because it looks a little crunched when tying to render it in a circular...
Read more >
Change the spaces between text - Microsoft Support
Kerning refers to the way spacing between two specific characters is adjusted. The idea is to give a better looking result by reducing...
Read more >
Change letter and line spacing in text - Adobe Support
To adjust kerning visually, click between two letters with the Type tool, and then press Option (macOS) or Alt (Windows) + left/right arrows. ......
Read more >
letter-spacing - CSS: Cascading Style Sheets - MDN Web Docs
The letter-spacing CSS property sets the horizontal spacing behavior between text characters. This value is added to the natural spacing ...
Read more >
3 Text Spacing Principles Every Designer Needs to Know
Kerning is the process of adjusting the space between individual letters. Often when you're working with type, you'll notice pairs of letters ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found