如何在程序创建的UIButton上添加左填充?

73

我在给UIButton添加左内边距方面遇到了麻烦。我有一个UIControlContentHorizontalAlignmentLeftUIButton,我希望文本显示在左侧,但是它太靠左了。当我设置边框时,效果不好。我想给文本大约5像素的内边距,就像在CSS中一样。我在谷歌上搜索解决方案,但没有找到特别针对UIButton的解决方案。


1
嗨!请考虑选择下面 @LucasChwe 的答案作为正确的答案。它似乎工作得更好... - Peter Lamberg
7个回答

133

titleEdgeInsets:按钮标题绘制矩形的内边距或外边距。

@property(nonatomic) UIEdgeInsets titleEdgeInsets

Discussion:使用此属性来调整并重新定位按钮标题的有效绘制矩形。您可以为每个边缘(上、左、下、右)指定不同的值。正值会缩小该边缘,即将其插入到按钮中心更近的位置。负值会扩展该边缘。使用UIEdgeInsetsMake函数构造此属性的值。默认值为UIEdgeInsetsZero。

Availability:在iOS 2.0及更高版本中可用。

声明于UIButton.h

试一下吧 :)

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

如果你正在使用自定义按钮,那么可能会涉及到内容插图和图像插图。

如果你是在寻找Swift方面的帮助,那么这段代码适用于Swift 3.0版本。

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

你也可以直接设置它。这对于想要使用一两个属性很有帮助。

myButton.titleEdgeInsets.top = 0
myButton.titleEdgeInsets.left = 5
myButton.titleEdgeInsets.bottom = 0
myButton.titleEdgeInsets.right = 0

@VanDuTran 我也遇到了一个问题,有些按钮可以使用,而有些按钮则不行。这可能是因为按钮上有限制条件,我逐个设置限制条件以升级为用户约束条件,然后最后再将它们删除。现在一些按钮可以正常工作,但仍存在一些按钮的问题.....如果您已经找到解决方案,请分享一下。 - K'am
1
@VanDuTran的解决方案是在视图控制器文件检查器中取消勾选autoLayout选项。 - K'am
2
Swift 2.0:myButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5.0, bottom: 0, right: 0) - Nathaniel
好的,我会把它加到帖子里。 - Ryan Poolos

30
如果您想通过接口构建器(IB)设置这些插入值,需要使用Xcode 8或更高版本,您将在“大小检查器”中找到这些插入设置,而不是"属性检查器"。
希望这可以帮助您。 输入图像描述

29

以下是更好的答案,用于:

  • 避免截断按钮标题
  • 避免标题超出按钮的视图范围
  • 使按钮框架与图像良好配合。

代码:

extension UIButton {
    func addLeftPadding(_ padding: CGFloat) {
        titleEdgeInsets = UIEdgeInsets(top: 0.0, left: padding, bottom: 0.0, right: -padding)
        contentEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: padding)
    }
}

用法:

myButton.addLeftPadding(10)

6
带有相等边缘填充的Swift版本:let sidePadding: CGFloat = 20.0 titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -sidePadding, bottom: 0.0, right: -sidePadding) contentEdgeInsets = UIEdgeInsets(top: 0.0, left: sidePadding, bottom: 0.0, right: sidePadding) - Arthur Clemens
感谢 @ArthurClemens - Zack Shapiro
在我的情况下,标题右对齐对我来说是不必要的。如果我留下一行 contentEdgeInsets = UIEdgeInsets(top: 0.0, left: padding, bottom: 0.0, right: padding) 就可以解决这个问题。 - Nike Kov

27

在 Xcode 6 中,你可以在 IB 中指定标题插入:

界面生成器边缘标题插入


2
这是一个很好的观点,但我发现使用上下文插入是最好的方法。 - SmileBot

7
以下是如何解决这个问题的另一个例子:
 [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];

此外,如果您的按钮带有图像,您可以简单地添加以下内容:
[self.myButton setImage:[UIImage imageNamed:@"YourImage.png"] forState:UIControlStateNormal];

Good luck!


4
_myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
_myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接