title: Placing UILabel along drawn line
body: <p>I want to place UILabel along drawn line with CAShapeLayer.</p>
  
  <p>Like that:</p>
  
  <p><a href="https://i.stack.imgur.com/c6kun.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c6kun.png" alt="enter image description here"></a></p>
  
  <p>Currently I can't understand how to calculate X and Y points to place the label at center of the line taking into calculated angle.</p>
  
  <pre><code>override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
  
      let linePath = UIBezierPath()
  
      linePath.move(to: CGPoint(x: initialPoint.x, y: initialPoint.y))
      linePath.addLine(to: CGPoint(x: finalPoint.x, y: finalPoint.y))
  
      let lineLayer = CAShapeLayer()
  
      lineLayer.lineWidth = lineWidth
      lineLayer.strokeColor = UIColor.white.cgColor
      lineLayer.lineCap = .round
      lineLayer.path = linePath.cgPath
  
      view.layer.insertSublayer(lineLayer, at: 0)
  
      // X &amp; Y don't place correctly
      let label = UILabel(frame: CGRect(x: (initialPoint.x - finalPoint.x) / 2, y: (initialPoint.y - finalPoint.y) / 2, width: view.frame.width, height: view.frame.height))
  
      let angle = atan2(finalPoint.x - initialPoint.x, finalPoint.y - initialPoint.y + navigationHeight)
  
      label.textColor = .white
      label.font = label.font.withSize(24.0)
      label.text = "Text here"
  
      label.transform = CGAffineTransform(rotationAngle: angle)
  
      view.addSubview(label)
  }
  </code></pre>