UIButton Text Margin / Padding

后端 未结 9 1126
名媛妹妹
名媛妹妹 2021-01-31 13:39

I have the following layout, and I\'m trying to add a padding to the left and right..

The controls are a disabled UIButton.

9条回答
  •  无人及你
    2021-01-31 13:53

    This option is also viable if its not too annoying to use a UIButton subclass

    class Button: UIButton {
        override var intrinsicContentSize: CGSize {
            get {
                let baseSize = super.intrinsicContentSize
                return CGSize(width: baseSize.width + titleEdgeInsets.left + titleEdgeInsets.right,
                              height: baseSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)
            }
        }
    }
    

    Then use titleEdgeInsets as desired

     let button = Button()
     ... configure button
     button.titleEdgeInsets = ...
    

提交回复
热议问题