How to align an UILabel to the right? - ios

I want to align an UILabel to the right. It called labeldistance. labeldistance appear if I autoresizing left (In this situation the label shown middle of the line). If I select right in autoresizing, labeldistance disappear. Do you have any solution ?

Try this
labeldistance.textAlignment = NSTextAlignmentRight;

Go to the UIStoryboard and select the UILabel you want to set the specific alignment.
If just aligning to the right, just use the arrow in the Size Inspector like this:
For resizing the UILabel horizontal, add the middle arrow:

Hopefully this screen shot helps - you want to add a constraint for the right alignment of your view (in this case, a UILabel) but since you're now using constraints, you also have to define the height, width, and vertical position.

Related

Add padding to sides of text in a button

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.

Creating UILabels with delete button

For my iOS app, I want to create UILabels with a button attached to the top right. If the user presses the button, the UILabel will be deleted. Here is a visual of what I want to create :
I know how to build the UILabel with the rounded corners and text but I'm a little lost on how to attach the buttons on the top right. Is the best way to create this to attach individual buttons to the labels? If I do follow this approach, wouldn't this get messy with constraints?
Rounded Labels
let label = UILabel() // But can be an outlet.
label.layer.cornerRadius = label.frame.height / 2.0
(Sorry, just saw that you knew this bit. I just got excited.)
Buttons
The constraints shouldn't be messy. However you want to align the buttons with labels you're going to need to specify the horizontal and vertical constraints, and possibly height and width constraints.
Just in case you're unfamiliar with IB:
Add a vertical constraint between the label and button. Any will do for now.
Add a horizontal constraint between the label and button. Any will do for now.
Double-click the vertical constraint to refine it; set the button's Centre Y to the label's Top anchor.
Double-click the horizontal constraint to refine it; set the Centre X to the label's Trailing anchor.
I don't think the centre of the button is exactly on the corner, so use the Constant property to adjust.

Force text vertically centred in UILabel

I have a UILabel whose size is determined by the distance of the view on top of it and the view at the bottom of it. The height of the UILabel will typically be different depending on the device. It seems that when Auto Layout stretches/crunches the UILabel's height, the text in it clings to the top of the UILabel as if the UILabel had a vertical alignment of text set to top. How can I force the text to be centred vertically?
While not understanding the perks of having Auto Layout stretch UILabel:s, I used an extra UIView in the place of my UILabel, and had Auto Layout stretch that one out instead. Then I put my UILabel centre of that containing UIView. No more constraints used for the UILabel. Seems to work great for now.
Click on the Label.
In the right side pane, select "Attributes Inspector".
For "Baseline" property, choose the option "Align Centers" to make the text centered vertically.

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.

Keep UILabel centered using Auto Layout

I'm trying to keep my UILabel centered horizontally. I have tried in IB setting leading and trailing, but that simply stretches out the entire label, and I need it to stay the same size. Everything is set up using Editor and the Pin function in IB and not coding. Suggestions?
If you want to keep all the time buttons together and aligned to the center. Put them all in their own view, arrange how you wish. Then fix the length and width of the view and center it in the container like Aaron suggested. I do believe you will need to make one more constraint. Perhaps pin the view to the top. Hope this helps.
Seems too obvious, but did you try nsconstraint of width = XXX. Label should say same width and the leading trailing should keep it put.
For keeping a UILabel horizontally centre, add the following constraints:-
Width constraint to the label.
Height constraint to the label.
Top/Bottom Constraint for placing the label vertically.
Horizontal centre in container.

Resources