lgc_ustc
lgc_ustc

Reputation: 1664

UIButton border padding

This looks like an easy enough question, but I can't seem to find a solution online. I use an UIButton's layer to make a border for it. And the border shows. The problem is that the paddings (top/right/bottom/left, obviously this term comes from the CSS world) of the border are too small, and I want to increase them. How do I do that?

Here is my code:

myButton.layer.cornerRadius = 10;
myButton.layer.borderWidth = 2;
myButton.layer.masksToBounds = YES;

And the button looks like above. Button border with tight padding

Upvotes: 3

Views: 2290

Answers (4)

MBH
MBH

Reputation: 16609

In iOS 15, if you use UIButtonConfiguration, It will be :

button.configuration.contentInset = UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4)

for older version:

button.contentEdgeInsets = UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4)

Upvotes: 0

Asif Mujteba
Asif Mujteba

Reputation: 4656

Use titleEdgeInsets Property of UIButton

        myButton.titleEdgeInsets = UIEdgeInsets(5, 5, 5, 5);

Adjust the insets as required! You can get more details about titleEdgeInsets from Apple Docs

Upvotes: 2

JohnV
JohnV

Reputation: 990

Try changing the contentEdgeInsets. These are the outer-most margins for the rectangle surrounding all of the button’s content.

For example: myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5);

Upvotes: 7

SKD
SKD

Reputation: 1493

Either you can reduce the button font size like,

myButton.titleLabel.font = [UIFont systemFontOfSize:14.0];

or just change the frame size of button in storyboard or programaticallyenter image description here

Upvotes: 0

Related Questions