ios 如何在 iphone 的 Button 中使用 UIEdgeInsets 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1781256/
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
How to use UIEdgeInsets property in Button in iphone
提问by Ankit
I am new to iOS application development and wanted to know how to use UIEdgeInsets
property in button so please guide me.
我是 iOS 应用程序开发的新手,想知道如何UIEdgeInsets
在按钮中使用属性,所以请指导我。
回答by Bryan Henry
First off, your question isn't very clear as to what you're attempting to achieve, so...ask a general question, get a general answer.
首先,您的问题对于您要实现的目标不是很清楚,所以......提出一个一般性问题,得到一个一般性答案。
UIButton has three properties of type UIEdgeInsets – contentEdgeInsets
, titleEdgeInsets
, and imageEdgeInsets
. You can use these properties to assign specific insets for all button content, including both the title and the image, or one or the other individually.
UIButton的有型UIEdgeInsets的三个属性- contentEdgeInsets
,titleEdgeInsets
和imageEdgeInsets
。您可以使用这些属性为所有按钮内容分配特定的插图,包括标题和图像,或者单独分配一个或另一个。
You create UIEdgeInsets values using the UIEdgeInsetsMake()
function. UIEdgeInsets encapsulates four different CGFloat values for the inset values of the top, left, bottom, and right (respectively) individually. Positive inset values shrink the content area, while negative inset values will effectively increase it.
您可以使用该UIEdgeInsetsMake()
函数创建 UIEdgeInsets 值。UIEdgeInsets 分别封装了顶部、左侧、底部和右侧(分别)的插入值的四个不同的 CGFloat 值。正插入值缩小内容区域,而负插入值将有效地增加它。
If you have a more specific question about how to use UIEdgeInsets with UIButton, I suggest you rethink how to ask it and try again. :)
如果您对如何将 UIEdgeInsets 与 UIButton 一起使用有更具体的问题,我建议您重新考虑如何提问并重试。:)
Edit(to address question in comment): Sounds like you would want to do the following, then:
编辑(以解决评论中的问题):听起来您想要执行以下操作,然后:
UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0, 7.0, 0.0, 0.0);
button.titleEdgeInsets = titleInsets;
回答by YannSteph
If you want an extensionfor Button and use simply, use this
如果您想要Button的扩展并简单地使用,请使用此
button.setInset(top: 0.0, left: 7.0, bottom: 0.0, right: 0.0)
button.setInset(顶部:0.0,左侧:7.0,底部:0.0,右侧:0.0)
extension UIButton {
/**
Creates an edge inset for a button
:param: top CGFLoat
:param: left CGFLoat
:param: bottom CGFLoat
:param: right CGFLoat
*/
func setInset(#top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
self.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)
}
}
回答by nuynait
Note that sometimes, you will not need to set the contentEdgeInsets. Just give the button a larger height and width constraints. The title itself will be center automatically.
请注意,有时您不需要设置 contentEdgeInsets。只需给按钮一个更大的高度和宽度约束。标题本身将自动居中。