3

This one is simple, but I'm kind of lost.

How can I make the Text in the middle (vertical and horizontal) of the circle? Or everything in the middle of the Svg (and then the text will be in the middle of the circle)

const size = width < height ? width - 32 : height - 16
const strokeWidth = 25
const radius = (size - strokeWidth) / 2
const circunference = radius * 2 * Math.PI

return (
    <Svg width={width} height={size}>
        <Text>
            Hey
        </Text>
        <Circle
            stroke="#2162cc"
            fill="none"
            cx={size / 2}
            cy={size / 2}
            r={radius}
            strokeDasharray={`${circunference} ${circunference}`}
            {...{ strokeWidth, strokeDashoffset }}
        />
    </Svg>
)
Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • If you want the `Hey` text to be inside `Circle` component then you will have to put this as child in `Circle` and then to keep it in center you can use css `alignItems: 'center', justifyContent: 'center'`...... OR else you will have to make the text component `position: absolute` (Not Recommended) – Milind Agrawal Sep 19 '19 at 14:31
  • @MilindAgrawal Could you please provide a full example? Not sure what you mean on your comment – Vencovsky Sep 19 '19 at 14:34
  • If you can create a demo link then maybe I can make the changes and share with you. – Milind Agrawal Sep 19 '19 at 14:36
  • @MilindAgrawal everything you need is in the question, and it's very simple, everything is from `react-native-svg` – Vencovsky Sep 19 '19 at 14:37

2 Answers2

7

You will have to use Text component from react-native-svg as shown below. Hope this helps

import React, { Component } from "react";
import Svg, { Circle, Text } from "react-native-svg";

class Demo extends Component {
  render() {
    const width = 100;
    const height = 100;
    const size = width < height ? width - 32 : height - 16;
    const strokeWidth = 25;
    const radius = (size - strokeWidth) / 2;
    const circunference = radius * 2 * Math.PI;

    return (
      <Svg width={width} height={size}>
        <Text
          stroke="purple"
          fontSize="15"
          x={size / 2}
          y={size / 2}
          textAnchor="middle"
        >
          Hey
        </Text>
        <Circle
          stroke="#2162cc"
          fill="none"
          cx={size / 2}
          cy={size / 2}
          r={radius}
          strokeDasharray={`${circunference} ${circunference}`}
        />
      </Svg>
    );
  }
}

export default Demo;

Milind Agrawal
  • 2,724
  • 1
  • 13
  • 20
1

Wrap the top SVG element in a View element, then, use position absolute to position the circle to act as a background (add 'padding' using top: 3, left: 3, if you need some padding for the circle not to be cut out).

Set alignItems:'center', justifyContent: 'center' on the most outer View element, to align the text to the center of the view.

Example

<View style={{alignItems: 'center', justifyContent: 'center'}}>
    <View style={{position: 'absolute'}}>
      <Svg width={width} height={size} >
          <Circle
              stroke="#2162cc"
              fill="none"
              cx={size / 2}
              cy={size / 2}
              r={radius}
              strokeDasharray={`${circunference} ${circunference}`}
              {...{ strokeWidth, strokeDashoffset }}
          />
      </Svg>
      <Text>
         Hey
      </Text>
   </View>
</View>

Note

This solution will work for any type of element you want to center, no matter how complex, not just for simple text.

Daniel B.
  • 2,491
  • 2
  • 12
  • 23