Vertical text in a Horizontal UIButton

前端 未结 5 1812
借酒劲吻你
借酒劲吻你 2021-01-21 10:01

I\'m using a vertical UIButton in a portrait app (Just a normal button that is of width 60 and Height 160)

I want to put the label Vetically down the button instead of a

5条回答
  •  猫巷女王i
    2021-01-21 10:54

    I add the same problem, for buttons in stack view So I needed something dynamic

    for Swift 4.2

    class VerticalButton: UIButton {
    
        override func titleRect(forContentRect bounds: CGRect) -> CGRect {
            var frame: CGRect = super.titleRect(forContentRect: bounds)
    
            frame.origin.y = 0
            frame.size.height = bounds.size.height
    
            return frame
        }
    }
    
    extension UILabel {
        @IBInspectable
        var rotation: Int {
            get {
                return 0
            } set {
                let radians = CGFloat(CGFloat(Double.pi) * CGFloat(newValue) / CGFloat(180.0))
                self.transform = CGAffineTransform(rotationAngle: radians)
            }
        }
    }
    

    and

        let button = VerticalButton.init(type: UIButton.ButtonType.custom)
        button.titleLabel?.textAlignment = .center
        button.titleLabel?.rotation = -90
    

提交回复
热议问题