How to keep text horizontally during rotating transform of containing view

97 views Asked by At

Making custom UI control, something like rotation wheel, looks like this: enter image description here

As you can see labels positioned "on radiuses", but i need place all labels horizontal, and keep em same way when I will rotate container view

1

There are 1 answers

0
Mosbah On

You can do something like this:

    let font = UIFont.systemFont(ofSize: 12)
    let attributes = [
        NSAttributedString.Key.font: UIConstants.CaptionFont,
        NSAttributedString.Key.foregroundColor: UIColor.accent
    ]

    for path in pathsForIndicators {
        let cgPath = path.cgPath
        let layer = CATextLayer()
        layer.frame = CGRect(x: cgPath.boundingBox.midX + offset + 22,
                             y: cgPath.boundingBox.midX + 10, width: 40, height: 40)
        layer.position = CGPoint(x: cgPath.boundingBox.midX + offset + 22,
                                 y: cgPath.boundingBox.midY + 10)
        layer.font = font
        let value = 0 // TODO
        let title = String(describing: value)
        let attributedString = NSAttributedString(string: title, attributes: attributes)
        layer.string = attributedString
        layer.isWrapped = false
        layer.alignmentMode = CATextLayerAlignmentMode.center
        layer.foregroundColor = indicatorColor.cgColor
        layer.shouldRasterize = true
        layer.needsDisplayOnBoundsChange = false
        layer.needsDisplay()
        layer.displayIfNeeded()
        textLayer.addSublayer(layer)
    }
    textLayer.backgroundColor = UIColor.clear.cgColor
    textLayer.bounds = bounds
    textLayer.position = centerPoint
    layer.addSublayer(textLayer)