Storyboard: centre text horizontally in UILabel - ios

I know how to do it programmatically:
textLabel.textAlignment = .center
But isn't it possible to do that in storyboard? Unbelievable.
There is an appropriate property called "Content Mode: Center" in the attributes inspector, but seems it doesn't work.

As #Sweeper has said in the comments. The UILabel has an Alignment property which you can find under the Attributes Inspector tab, set the value to Center represented by horizontally centred dashes.

Related

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.

iOS Swift Text Fill Screen

I have taken a label and dragged it onto a blank UIView.
I have changed the text orientation with
codeLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
but I now want the text to fill the screen, I have browsed the web and found this but it doesnt seem to work
codeLabel.adjustsFontSizeToFitWidth = true
Add a width equal constraint between the UILabel and its parent UIView. Do that by draging from the UILabel to the UIView.
What you want to achieve is to let the UILabel with equals to the UIView height. but what you have so far is an equal widths constraint.
Select that constraint by clicking it and change the value next to "Second Item" to Superview.Height.
To let font fit the screen:
select the label.
set a very large font. example 100.
change the value of Autoshrink to Minimum font size.
set a small value there. example 5.

set uibutton text alignment storyboard

I know that in code I can do [myButton.titleLabel setTextAlignment: NSTextAlignmentRight]; but how would I do this in Storyboard in User Defined Runtime Attributes?
You can set the alignment of content (text or image) within your UIButton in the Attributes Inspector Panel.
The menu is "Control", you can set Horizontal & Vertical Alignment
How to make UIButton's text alignment center? Using IB
This post shows how to do it in storyboard.
You can also adjust the constraints of the content, title or image in the size inspector of the button:
In my case, I was only able to really centralize the character "+" and "-" after setting a value for the bottom of the title.

How to align an UILabel to the right?

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.

Show background color in UILabel only as long as the text is

I've set the backgroundColor via the attributes tab for the view controller in my Storyboard. But I want to show the background color only in the length of the text which is filled into the label, not in the whole possible length.
I can not attach any image, so here is what I would like to have:
Try the following steps:
Select the UILabel in the Storyboard.
Open the "Size Inspector."
In the "Label" section, make sure "Explicit" is unchecked.
In the list of "Constraints," make sure that no constraint provides a value for the width property. Also, remove any constraints that pin the right edge of the UILabel to the UIImageView as this will implicitly give Auto Layout a fixed width for the label.
Add an "Equal Widths" constraint between the UILabel and the UIImageView. This can be achieved by control-dragging from the label to the image view in the Storyboard and selecting "Equal Widths" from the menu that appears.
Select either the label of the image view. Open the "Size Inspector" again double click on the box that corresponds to the constraint that you just created. In the "Relation" drop down menu, choose "Less Than or Equal".
This will cause the UILabel to dynamically change it's width depending on the width of the text drawn inside it. When you set the backgroundColor property, it will still fill the label by will only show behind the text. The Equal Widths constraint will make sure that the width of the label never exceeds the width of the image view.

Resources