UIButton Text Margin / Padding

后端 未结 9 1125
名媛妹妹
名媛妹妹 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 14:07

    You can also set the inset values from the Interface Builder Size Inspector inside a Storyboard or xib.

    Interface Builder Size Inspector

    0 讨论(0)
  • 2021-01-31 14:10

    I found an easy/hacky way to add borders to text buttons (and have left/right margins):

    1. Create button with title.

    2. Place button in storyboard, align where you want and then add a forced width constraint that is an even number (I used 20, so it adds 10 points on each side). This will force the border around the width you created.

    3. Use code to create a border. eg:

      myTextButton.backgroundColor = .clear
      myTextButton.layer.cornerRadius = 5
      myTextButton.layer.borderWidth = 2
      myTextButton.layer.borderColor = UIColor.white.cgColor
      
    4. Set left titleInset to 2 via editor (now under Size Inspector) or by code. This seems to center the text, but this value may be different for various texts and text sizes.

    This post is for Xcode 8.1 and Swift 3.

    0 讨论(0)
  • 2021-01-31 14:11

    With the above solutions, some of the text were cut out if you have a border around the button. For instance, a button label named "Delete something" ends up showing "Dele...ing". If you are having this problem, this is the solution:

    aButton.contentEdgeInsets = UIEdgeInset.init(top: 0, left: 8, bottom: 0, right: 8)
    
    0 讨论(0)
提交回复
热议问题