Add padding to sides of text in a button - ios

Using Xcode's storyboard, how can I add padding to the left and right of a UIButton's text? I can't for the life of me figure out how to do this.
Here is my button:
I want the button to "hug" the text and then give padding of 32px on the left and right sides of the button.
But when I try to add a Title Inset, it just squeezes the text to a point where it's almost not showing:
So how do I adjust the button's padding properly?

Ensure following things are done for the Button in storyboard.
Update Content insets value for Left and Right as 32px in Size
Inspector in storyboard.
Also, ensure Content Compression Resistance priority -
Horizontal value is high (751) for safer side.
Remove if you have any width constraint for Button, if possible.
Do not update Title insets. It will not work as you expected.
I have tested this. It's working for me.

Related

Left aligning image on centered UIButton

What is a good way to have the image edge insets of a UIButton be left aligned while having the title completely centered? Such as the following:
The image would have a constant left inset, but the title would always be centered in the entire button. Most solutions that I have found date back 4 or 5 years, and they are based on manually calculating the necessary inset of the image while keeping a center alignment for the title.
Are there really no easy ways of doing this yet?
You can set asset left aligned by using an image for your button and set horizontal alignment to left. For the title, you could add an additional UILabel and constraint it in a horizontal center-aligned manner.
you can set edge to change position of text and image of button
set Left Top Bottom and right according your requirement.

UILabel right next to UIImage using autolayout

How would I go about creating a layout like this using autolayout?
The icon is UIImage, text is UILabel.
The label text can vary in width, but will always be single-line. How do I force the label to scale with the text horizontally and UIImage to be immediately to the left of the label?
I have tried using attributedText. I also tried views as containers and a horizontal stack view, but failed to get the result I wanted. What are the other options?
That should be absolutely no problem - just put in the image, make layout constraints to the left and top of the view, then add the label right to it, and make layout constraints between the right side of the image and the left side of the text.
To restrict the label width you can add a constraint from the right side of the label to the right side of the view, if you want to make the label as small as possible with a greater-than sign.
EDIT
If the whole should be bound to the right border of the superview, put both in a view, align the view to the right border of the superview, and make a constraint from the right side of the label to the right side of the view. The view will resize accordingly when the label resizes.
I went for a solution where I added a third view as the leftmost element in a horizontal stackview and set the stackview's distribution as 'equal centering'. It actually fit well with the rest of the planned layout (all elements are used efficiently). Thank you for your help guys.

Xcode IB auto sizing?

Everything looks fine in IB. When I run my app using an iPhone5/iPhone6/iPad sims, it's completely mangled.
In IB:
In iPhone6 sim:
In the above sim, the buttons are cut. The textfield and textview are also cut. For the TF & TV, they are aligned center but trail off the edges on both sides.
I have the buttons width set
Editor > Pin > Widths Equally
The title label is set with a
Horizontal Center in Container
constraint
The textfield and textview also have
Horizontal Center in Container
and
Editor > Pin > Width
Is there some way to fix this?
-- EDIT --
After a few tries with constraints, looks like I have everything working except the two buttons.
Current listing of constraints:
As suggested by others, you should consider using the Auto-layout feature if you plan on constructing your views using the IB.
Here are some tutorial links:
RayWenderlich.com Part One
Youtube video This one covers a bit of size clases
Hope they help.
UPDATED:
I've read your updated post, you need to add width and height constraints. The view you see in the IB right now is 600 by 600 points, and the simulator one is smaller, which means that if you leave it as is, when you run the app you'll only see what the iphone screen has the capacity to show.
You need to add more constraints than what you used, try defining an equal width for the buttons, and assign the left one a left margin constraint, the right one a right margin constraint, give both of them vertical spacing constraint related to the text view or long label, the text view or long label should have left and right margin constraints, a height constraint and a top constraint to the textfield, the textfield should have a vertical spacing constraint to the label, also left and right and height constraint, and last the label should have leff, right and height constraints plus a top constraint to the main view.
I feel that those are all you need, but Xcode will through warnings at you if it feels you are missing something.
Try it, and let us know.

Need assistance setting two buttons of equal width, side by side Autolayout

Using auto layout I am trying to create two buttons on the bottom like this
After dragging two buttons I set constraints like this
On Back button I set Leading Space to Container Margin and Bottom Space to Bottom Layout Guide
On Go To Settings button I set Trailing Space to Container Margin and Bottom Space to Bottom Layout Guide
Then I ctrl+Drag from Back button to Go To Settings button and set the Equal Widths constraint and I get this.
Then I update the Back button width same as Go To Settings button which is 101 and all constraints turn to blue.
But I want both buttons to cover half of the screen no matter what the size of screen is, same as shown in first screen shot, How to accomplish this?
you can done it using equal width to 2nd button like this
and the output is
Here is apple guide
You can have the equal widths constraint between the buttons, but you also have to tell them which is that width you need.
So drag from a button to the superview, add an equal widths constraint. Then in the properties of the constraint you've added, set the multiplier to 0.5, this way your buttons should be on half the screen.
You need to add one more constraint - set horizontal space between buttons to 0
You can followed the layout changes. you will get what you want.
Layout for Back button.
Layout for Go to Setting button.

UIButton changing width when label changes

I'm writing a calculator app with a shift key. When the shift key is pressed, some of the UIButtons' labels should change. The problem is that when they do, the widths also change and, because of certain design constraints, other buttons will resize themselves too. How to I lock the width of a button (either pragmatically or in the storyboard builder) so that it will never change (regardless of the contents of the button)
Thanks for any help in advance.
Buttons, like labels have an intrinsic size that's set by the width of their text plus some padding. If you want them to have a fixed width, then in IB, just give each button an explicit width from the menu, Editor --> Pin --> Width.
Have you tried using [button sizeToFit]? Call it after you resize the subviews within and then you will likely want to apply some "padding" as this will size the view to exactly fit it's subviews.
UIView sizeToFit

Resources