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.
Related
I have a UITextField in an Xcode storyboard, with horizontal alignment to centre and a top and bottom alignment:
however the field is too small, so I changed the width. I keep the horizontal align to centre so it's not using numerical values like this:
However, when I run, or press Editor > Update Frames, I notice that the field shrinks again! The width returns to 25. How can I keep the width wide, say 250, without it reverting??
You have to add a width constraint to it, otherwise it just has it's intrinsic size. Pressctrl and drag a horizontal line from the field to itself. You can then adjust the constant value of this constraint.
You can also add a placeholder text to it like "Enter text here..." in attributes inspector > Placeholder.
It changes the TextField intrinsic content size and allows you to see the result without need to add constraints.
Update constraint instead of Update Frames.
I am very new to Swift and I don't know how to make my UILabel show all text.
Here is how my label looks like in my storyboard:
As you see, part of the text is omitted. I can show all the text by dragging the UILabel to resize. However, during the runtime, if the text is set dynamically, how can I resize the label so that it can show more all of the text?
I tried UILabel.sizeToFit() but that doesn't seem to change anything.
you can do this by setting numberOfLines to 0
Example
myLabel.text = "whatever"
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
And you have so set frame according to it
Refer : to this link
YourUILabel.sizeToFit() is right but that is not all of it..
You need to set the number of lines and the kind of line break before calling .sizeToFit()..
YourUILabel.numberOfLines equal to 0 for unlimited number of
lines...
YourUILabel.lineBreakMode equal to NSLineBreakMode dot(.)
ByWordWrapping for example..
If you are not resizing the UILabel frame using autolayout you can change the Autoshrink option by selecting your label from the storyboard and adjust this option on the inspector pane.
.
Why do it the old way.. Let's use autolayout. If you haven't used it yet. Lets begin - Steps :
Create a UIlabel and set it's width to any amount you want it on screen.
Select the label go to Editor menu -> Pin and one by one pin Leading Trailing and Top space to Superview (not the bottom one).
Since you have fixed width and you have dynamic height so also pin Width.
Go to Attribute inspector tab and set lines to 0 (means unlimited).
Go To size inspector and set Content Hugging priority(250/251) to low and Content Resistance priority to high (say - 750).
And you are done.!!(not a single line of code)
You first have to calculate the Height of label according to the length of the Text you want to set and then accordingly increase the number of lines
as
yourLabel.numberOfLines = "Your calculation number"
and then Set the Frame of the Label according to the Height calculated
and
if you cannot calculate the height of the Label then i would say use the TextView it will automatically add a scroll to the text and you can view all the text.
I added a button to my view in viewcontroller and I gave it only positioning constraints (which are top space and leading space). In my button I added a set, an image and a title which is three four words. I have other views in the same horizontal row. I set their title and edge insets programmatically so both are aligned to center and title is just below the button.
How can I set its title dynamically so it will show full title? I have already used sizeToFit function that doesn't help.
Determine the size of your button title label using boundingRectWithSize:options:context:, then set the UIButton.frame accordingly.
Apple docs - boundingRectWithSize:options:context:
Edit:
Autolayout resize button based on text and have textfield fill available space
I already set UIImageView.width(Green Color Background) == UILabel.width + 20 in my tableView cell ,but when the label's content changing , the label 's height is changing at first ,but I want the label to grow wider first , if it can't be more wider then grow higher. what 's the problem here
Hi yes you can do it. I am explaining below how can u do it with auto layout.
At the time of applying constraint, instead of giving hight and width
to UIImageView just try to give it three constraint with UILabel which
are " Equal Width, Equal Height and Aspect Ratio"
For Applying these three constraints with UILabel the shortcut is just
drag and drop your mouse cursor from UIImage to UIlabel with right
click only, so you can see above three options. After applying it ,give
UIlabel Leading and trailing constraint( or whatever u want) and most important thing, set
your UIlabel to size to content fit. For UILabel,shortcut is just
click on UILabel and click on top toolbar's tab EDITOR and you will
find option for setting UILABEL as size to content fit so after
setting it UILabel will automatically set its frame as per its content
and so as UIImageView.
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.