简体   繁体   English

如何在以编程方式创建的UIButton上添加padding-left?

[英]How to add padding-left on a UIButton created programmatically?

I am having a trouble adding left padding on a UIButton . 我在UIButton上添加左边距时遇到问题。 I have a UIButton with UIControlContentHorizontalAlignmentLeft . 我有一个UIButtonUIControlContentHorizontalAlignmentLeft I want the text to be displayed on the left side but it is too left. 我希望文本显示在左侧,但它太左了。 when I give the border, it doesn't look good. 当我给边框时,它看起来不太好。 I would like to give some padding on the text about 5px like in CSS. 我想在CSS中给出一些关于5px的文本填充。 I googled for the solution but can't find one particularly for UIButton . 我搜索了解决方案,但找不到特别针对UIButton的解决方案。 Thanks in advance for help. 在此先感谢您的帮助。

titleEdgeInsets The inset or outset margins for the edges of the button title drawing rectangle. titleEdgeInsets按钮标题绘制矩形边缘的插入或开始边距。

@property(nonatomic) UIEdgeInsets titleEdgeInsets @property(非原子)UIEdgeInsets titleEdgeInsets

Discussion Use this property to resize and reposition the effective drawing rectangle for the button title. 讨论使用此属性可以调整按钮标题的有效绘图矩形并重新定位。 You can specify a different value for each of the four insets (top, left, bottom, right). 您可以为四个插图(顶部,左侧,底部,右侧)中的每一个指定不同的值。 A positive value shrinks, or insets, that edge—moving it closer to the center of the button. 正值会缩小或缩小,使其靠近按钮的中心。 A negative value expands, or outsets, that edge. 负值会扩展或偏离该边缘。 Use the UIEdgeInsetsMake function to construct a value for this property. 使用UIEdgeInsetsMake函数为此属性构造值。 The default value is UIEdgeInsetsZero. 默认值为UIEdgeInsetsZero。

Availability Available in iOS 2.0 and later. 可用性适用于iOS 2.0及更高版本。

Declared In UIButton.h 在UIButton.h中声明

Give this a try :) 试一试:)

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

Also if you're using a custom button there is such a thing as Content Insets and Image Insets. 此外,如果您使用自定义按钮,则会出现内容插入和图像插入等内容。

Incase you've made it here looking for Swift. 如果你在这里寻找斯威夫特。 This is valid Swift 3.0 😃 这是有效的Swift 3.0😃

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

You can set it directly as well. 您也可以直接设置它。 It is helpful if want to use one or two properties. 如果要使用一个或两个属性,这将很有帮助。

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

In Xcode 6 you can specify title inset in IB: 在Xcode 6中,您可以在IB中指定标题插入:

界面构建器边标题插入

Post Xcode 8 if you want to set it these insets through interface builder (IB),you will find these inset settings in size inspector instead of attribute inspector. 发布Xcode 8如果要通过界面构建​​器(IB)将其设置为这些插入,您将在尺寸检查器而不是属性检查器中找到这些插入设置。

Hope this helps. 希望这可以帮助。

在此输入图像描述

Here is a better answer to: 这是一个更好的答案:

  • avoid truncating the button title 避免截断按钮标题
  • avoid title from extending beyond the button's view 避免标题超出按钮的视图
  • make the button frame work well with an image. 使按钮框架与图像一起使用。

Code: 码:

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)
    }
}

Usage: 用法:

myButton.addLeftPadding(10)

Here is another example how to resolve 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 a image you can simply add: 此外,如果您的按钮有图像,您只需添加:

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

Good luck! 祝好运!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM