How to use UIEdgeInsets property in Button in iphone

大城市里の小女人 提交于 2019-12-03 05:49:24

问题


I am new to iOS application development and wanted to know how to use UIEdgeInsets property in button so please guide me.


回答1:


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.

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.

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. :)

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;



回答2:


If you want an extension for Button and use simply, use this

button.setInset(top: 0.0, left: 7.0, bottom: 0.0, right: 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)
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/1781256/how-to-use-uiedgeinsets-property-in-button-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!