UIButton Text Margin / Padding

后端 未结 9 1144
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题