【问题标题】:How to keep text horizontally during rotating transform of containing view如何在包含视图的旋转变换期间保持文本水平
【发布时间】:2019-01-18 19:37:08
【问题描述】:

制作自定义 UI 控件,例如旋转轮,如下所示:

您可以看到标签位于“半径上”,但我需要将所有标签水平放置,并在旋转容器视图时保持相同方式

【问题讨论】:

  • 你是指手机旋转的时候吗? “水平”是什么意思?水平居中?
  • 不,我的意思是当我使用类似的东西旋转“轮子”时:{...container.transform = CGAffineTransform.identity.rotated(by: -angleDifference)..}
  • Horizo​​ntally 表示文本的基线是水平的

标签: ios swift image-rotation catransform3d


【解决方案1】:

你可以这样做:

    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多