ios 在 Swift 中以编程方式更改 UIButton 的文本(填充)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31353302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:49:20  来源:igfitidea点击:

Change a UIButton's text (padding) programmatically in Swift

iosiphoneswiftuibutton

提问by KellysOnTop23

Still learning Swift and don't know Objective-C. I saw that in order to changing a button's text programmatically requires the use of titleEdgeInsetsbut I am not really sure how to use it.

还在学习 Swift,不知道 Objective-C。我看到为了以编程方式更改按钮的文本需要使用,titleEdgeInsets但我不确定如何使用它。

I would like to change the text in the button (padding) in the bottom and both the left and the right.

我想更改底部以及左侧和右侧的按钮(填充)中的文本。

Thanks for any responses and/or examples!

感谢您的任何回复和/或示例!

回答by 7stud

Still valid for: iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2

仍然适用于: iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2



iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5:

iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5

The method titleEdgeInsetsdidn't work for me. My button's frame tightly hugs the text (I didn't specify a frame size), and the frame has a red background color. After doing:

该方法titleEdgeInsets对我不起作用。我的按钮的框架紧紧地拥抱文本(我没有指定框架大小),并且框架具有红色背景色。做完之后:

 myButton.titleEdgeInsets = UIEdgeInsetsMake(10,10,10,10)

the red background shrunk inwards by 10 pixels on each side, which meant that now some of the text was outside the red background. Using negative values had no effect on the original frame size.

红色背景每边向内收缩 10 个像素,这意味着现在有些文本在红色背景之外。使用负值对原始帧大小没有影响。

I was able to get padding around the text, effectively making the frame bigger, by doing:

通过执行以下操作,我能够在文本周围进行填充,从而有效地使框架更大:

myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5) 

回答by Dev2rights

iOS 10 and Swift 3 example

iOS 10 和 Swift 3 示例

Tested and works on IOS 11 and Swift 4.

在 IOS 11 和 Swift 4 上测试并运行。

refineButton.contentEdgeInsets = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)

回答by Ali Raza

You can add padding from top, left, right and bottom by doing these lines of code.

您可以通过执行这些代码行从顶部、左侧、右侧和底部添加填充。

button.titleEdgeInsets.left = 10; // add left padding.
button.titleEdgeInsets.right = 10; // add right padding.
button.titleEdgeInsets.top = 10; // add top padding.
button.titleEdgeInsets.bottom = 10; // add bottom padding.

回答by Nik Kov

You can also do this in the storyboard:
enter image description here

您也可以在故事板中执行此操作:
在此处输入图片说明

回答by Hannes

For iOS 9.1/Xcode 7.1/Swift 2.1

适用于 iOS 9.1/Xcode 7.1/Swift 2.1

@IBOutlet weak var ratingButton: UIButton!

override func viewDidLoad() {
    ratingButton.contentEdgeInsets = UIEdgeInsets(top:15,right:10,bottom:15,left:10)

    super.viewDidLoad()
}

回答by Ahmet Gokdayi

Or you can use a custom class if you don't want to define the properties everytime you create a button;

或者,如果您不想每次创建按钮时都定义属性,则可以使用自定义类;

class UIPaddedButton: UIButton {
  let padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  override init(frame: CGRect) {
    super.init(frame: frame)
  }

  override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
    return contentRect.inset(by: padding)
  }
}

And when initializing;

并在初始化时;

let btn = UIPaddedButton()