gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:58:33 25 4
gpt4 key购买 nike

我在 UIButton 上添加左填充时遇到问题。我有一个带有 UIControlContentHorizo​​ntalAlignmentLeftUIButton。我希望文本显示在左侧,但它太左了。当我给出边界时,它看起来不太好。我想像 CSS 一样在文本上添加一些填充,大约 5px。我在谷歌上搜索了解决方案,但找不到专门针对 UIButton 的解决方案。在此先感谢您的帮助。

最佳答案

titleEdgeInsets The inset or outset margins for the edges of the button title drawing rectangle.

@property(nonatomic) 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. The default value is UIEdgeInsetsZero.

Availability Available in iOS 2.0 and later.

Declared In 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

关于swift - 如何在以编程方式创建的 UIButton 上添加 padding-left?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10589150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com