Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UIEdgeInsets property in Button in iphone

Tags:

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

like image 495
Ankit Avatar asked Nov 23 '09 05:11

Ankit


People also ask

What does UIEdgeInsets do?

Edge inset values are applied to a rectangle to shrink or expand the area represented by that rectangle. Typically, edge insets are used during view layout to modify the view's frame. Positive values cause the frame to be inset (or shrunk) by the specified amount.

How do I use imageEdgeInsets?

The rule when it comes to titleEdgeInsets or imageEdgeInsets is to add equal and opposite offsets to the left and right insets. So if you add for example 8 points to the left title inset, you need to apply -8 points to the right inset.

What is Contentedgeinsets?

The inset or outset margins for the rectangle surrounding all of the button's content.


2 Answers

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)
    }
}
like image 21
YannSteph Avatar answered Oct 11 '22 23:10

YannSteph


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;
like image 135
Bryan Henry Avatar answered Oct 12 '22 01:10

Bryan Henry