How to add a registration to the left on a UIButton, created programmatically?

I'm having trouble adding a left pad on UIButton . I have a UIButton with a UIControlContentHorizontalAlignmentLeft . I want the text to appear on the left side, but it's too left. when I give the border it doesn't look very good. I would like to give some addition to the text about 5px, as in CSS. I googled for a solution, but can't find it especially for UIButton . Thanks in advance for your help.

+47
padding xcode swift uibutton
May 14 '12 at 18:40
source share
6 answers

titleEdgeInsets Inserts or source fields for the edges of the rectangle of the draw button header.

@property (non-nuclear) UIEdgeInsets titleEdgeInsets

The discussion . Use this. property for resizing and changing the correct drawing rectangle for the button name. You can specify a different value for each of the four inserts (top, left, bottom, right). A positive value is compressed or pasted that intersect it closer to the center of the button. a negative value expands or goes around this edge. Use the UIEdgeInsetsMake Function to create a value for this property. The default value is UIEdgeInsetsZero.

Availability Available in iOS 2.0 and later.

Announced at UIButton.h

Try it :)

 [myButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 0.0)]; 

Also, if you use a custom button, there is such a thing as pasting content and pasting images.

Go, you did it here, looking for Swift. This is a valid Swift 3.0 😃

 myButton.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 0.0) 

You can also install it directly. Useful if you want to use one or two properties.

 myButton.titleEdgeInsets.top = 0 myButton.titleEdgeInsets.left = 5 myButton.titleEdgeInsets.bottom = 0 myButton.titleEdgeInsets.right = 0 
+81
May 14 '12 at 18:45
source share
— -

In Xcode 6, you can specify an insert in IB:

Interface builder edge title inset

+23
Feb 04 '15 at 2:05
source share

Here is the best answer to the question:

  • avoid trimming button names
  • avoid title button representation
  • make the button frame work well with the image.

the code:

  // Obj-C CGFloat padding = 10.0f; [myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, padding, 0, -padding)]; [myButton setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, padding)]; // Swift let padding: CGFloat = 10.0 myButton.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: padding, bottom: 0.0, right: -padding) myButton.contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: padding) 
+15
May 08 '15 at 20:00
source share

Publish Xcode 8, if you want to install these inserts through the Interface Builder (IB), you will find these insert settings in the dimension inspector instead of the attribute inspector.

Hope this helps.

enter image description here

+13
Jan 05 '17 at 10:03 on
source share

Here is another example of how to solve this:

  [self.myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; float padding_button = 6.0f; UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0f, padding_button, 0.0f, -padding_button); UIEdgeInsets contentInsets = UIEdgeInsetsMake(padding_button, 0.0f, padding_button, 0.0f); CGFloat extraWidthRequiredForTitle = titleInsets.left - titleInsets.right; contentInsets.right += extraWidthRequiredForTitle; [self.myButton setTitleEdgeInsets:titleInsets]; [self.myButton setContentEdgeInsets:contentInsets]; [self.myButton sizeToFit]; 

Also, if your button has an image, you can simply add:

 [self.myButton setImage:[UIImage imageNamed:@"YourImage.png"] forState:UIControlStateNormal]; 

Good luck

+6
Jun 01 '14 at 14:20
source share
 _myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; _myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); 
+3
May 11 '16 at 9:06 a.m.
source share



All Articles