【问题标题】:How can you rotate text for UIButton and UILabel in Swift?如何在 Swift 中旋转 UIButton 和 UILabel 的文本?
【发布时间】:2015-04-27 08:45:41
【问题描述】:

如何旋转UIButtonUILabel 的文本? 90度,180度。

注意:在将其标记为重复之前,我有意将我的问题建模为该问题的 Swift 版本:How can you rotate text for UIButton and UILabel in Objective-C?

【问题讨论】:

    标签: ios swift uibutton rotation uilabel


    【解决方案1】:

    我的回答格式与this answer 类似。

    这是原始标签:

    顺时针旋转 90 度:

    yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
    

    旋转 180 度:

    yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
    

    逆时针旋转90度:

    yourLabelName.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
    

    做同样的事情来旋转一个按钮。值得庆幸的是,触摸事件也会旋转,因此按钮仍然可以在其新边界内单击,而无需执行任何额外操作。

    yourButtonName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
    

    注意事项:

    CGAffineTransform 的文档

    基本格式是CGAffineTransform(rotationAngle: CGFloat),其中rotationAngle 的单位是弧度,而不是度数。

    一个完整的圆(360 度)有 2π 弧度。 Swift 包含有用的常量CGFloat.pi

    • CGFloat.pi = π = 180 度
    • CGFloat.pi / 2 = π/2 = 90 度

    自动布局:

    自动布局不适用于旋转视图。 (请参阅Frame vs Bounds 了解原因。)这个问题可以通过创建自定义视图来解决。 This answer 展示了如何为 UITextView 执行此操作,但它与标签或按钮的基本概念相同。 (请注意,您必须删除该答案中的 CGAffineTransformScale 行,因为您不需要镜像文本。)

    相关

    【讨论】:

    • 我对我的 imageview 做了同样的事情。但是我的图像被缩放(小或大)。如何为 imageviews 做......?
    【解决方案2】:

    如果您经常这样做,您将不会进行扩展。这也将允许您将视图旋转 0-360 度。

    extension UIView {
        func rotate(degrees: CGFloat) {
            rotate(radians: CGFloat.pi * degrees / 180.0)
        }
    
        func rotate(radians: CGFloat) {
            self.transform = CGAffineTransform(rotationAngle: radians)
        }
    }
    

    然后你可以简单地在你的视图上调用旋转

    myView.rotate(degrees: 90)
    

    【讨论】:

      【解决方案3】:

      迅速

      顺时针旋转 90 度:

          var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))
          
          rotateLabel.textAlignment = .Right
          
          rotateLabel.text = "Hello!"
          
          self.view.addSubview(rotateLabel)
          
          rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
          
          rotateLabel.frame = CGRectMake(100, 0, 28, 159)
      

      逆时针旋转90度:

          var rotateLabel: UILabel = UILabel(frame: CGRectMake(100, 0, 28, 159))
          
          rotateLabel.textAlignment = .Right
          
          rotateLabel.text = "Hello!"
          
          self.view.addSubview(rotateLabel)
          
          rotateLabel.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
          
          rotateLabel.frame = CGRectMake(100, 0, 28, 159)
      

      【讨论】:

      • 你必须重新分配框架?!
      【解决方案4】:

      我建议使用带有度数和逆时针/顺时针选项的扩展

      func rotate(degrees: Int , clockwise: Bool)
      {
          let x  = 180 / degrees
          if (clockwise)
          {
              self.transform = CGAffineTransform(rotationAngle: CGFloat.pi / CGFloat(x))
          }
          else
          {
              self.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / CGFloat(x))
          }
      }
      

      我将其用作extension UILabel {},但这显然取决于您

      【讨论】:

        【解决方案5】:

        改编了 Ananthu R Krishnan 的答案以适应 Swift 3.1。

        逆时针旋转90度:

        let rotateLabel: UILabel = UILabel(frame: CGRect(x:15, y:66, width:28, height:159))
        rotateLabel.textAlignment = .right
        rotateLabel.text = "TEXT"
        self.view.addSubview(rotateLabel)
        rotateLabel.transform = CGAffineTransform(rotationAngle: CGFloat(-(Double.pi / 2.0)))
        rotateLabel.frame = CGRect(x:15, y:66, width:28, height:159)
        

        【讨论】:

          【解决方案6】:

          如果您使用自定义字体,您可能需要:

          rotateLabel.sizeToFit()
          

          【讨论】:

            猜你喜欢
            • 2011-09-13
            • 1970-01-01
            • 1970-01-01
            • 2016-11-25
            • 1970-01-01
            • 2015-01-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多