Cannot set dynamic height based on text - ios

I have a textview within which is a label. The label gets texts of varying lengths from the server.
Now the texts from the server are displayed like so on my label (which is in a textview)
In order to increase the textview height as per the text in the label, I did this...
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
incidentTextView.translatesAutoresizingMaskIntoConstraints = true
incidentTextView.sizeToFit()
actionTextView.translatesAutoresizingMaskIntoConstraints = true
actionTextView.sizeToFit()
submitByTextView.translatesAutoresizingMaskIntoConstraints = true
submitByTextView.sizeToFit()
}
But this gives me the result as shown below..
How can I align the fields like the first image and yet have the height of the textview increased dynamically as per the text in the label..?
EDIT 1: I cannot see the scrolling option also for UIView..

You could use UILabel instead UITextView and just set numberOfLines = 0.
Make sure you used correct constraints to align UILable proper.

Related

How to modify view controller after pressing a button?

Let's say that if you press the button from the bottom of the screen after typing something in the account text field, you would be required to also enter the password like in the second image.
How should I do this? I don't think that creating a new view controller would be good. So, should I somehow modify the same view controller?
How could I add the new password text field under the account text field?
Keep in mind that they are still centered. Hiding and unhiding wouldn't work in this case and I also need to modify more things than only adding that text field.
First, create a UIView with everything you need on them. in this example I will have only two text fields and they are all color coded.
The view needs to be centered both horizontally and vertically, with width and height. Set identifiler for the height constraint to be updated later. Set the clip to board to true so that when we redeuce the height of the view, text fields below will hide. The settings for the view will be like this
For your text fields, they must have a constant padding to top. In my example, they are set to be in center horizontally, having a constant height, width and padding to opp.
Now, all you need to do is to get the height of the view from code and set the height to show or hide the text fields.
var flag = true
#IBAction func click(_ sender: Any) {
if flag {
flag = false
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 60
}
} else {
flag = true
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 128
}
}
}
Here is the code running in simulater.
another option is you can make tableView at Center, you need to create tableview height constraint outlet,
then you can maintain a counter how many time you want to add View, the counter should be return in tableView numberOfRowsInSection,
and you can make this view in Prototype Cell or using NIB, then just adjust header label text, textField placeholder and text on specific cell index,
when you increase or decrease counter update tableView Constraint, example you have a cell height as 50, in first case when you have on cell in tableView, you can set your tableView height constraint's constant as 50, when cells are two the 100 and so on.....
I think it's a simple logic

intrinsically sizing from core data (swift 3)

I have a name label in a cell that is intrinsically sized so that the left anchor of the time label may always be 5 pixels from the left anchor of the name label. However when I introduce names from core data, it takes the initial setup name and uses that name to intrinsically size the name label frame.
Here is where I create the label view.
let nameLabel: UILabel = {
let nlabel = UILabel()
nlabel.text = "Barack Obama"
nlabel.font = UIFont.boldSystemFont(ofSize: 12)
nlabel.textAlignment = .left
nlabel.frame.size = nlabel.intrinsicContentSize
nlabel.backgroundColor = UIColor.blue
print("nlabel frame \(nlabel.frame.size)")
return nlabel
}()
nlabel.text for each cell is equal to cell?.friend?.name
I am successfully able to load the names into the cell however the frame is still sized as if nlabel.text was equal to the first nlabel.text.
Any suggestions?
Assuming you are using Auto Layout, remove this line:
nlabel.frame.size = nlabel.intrinsicContentSize
And add this line:
nlabel.translatesAutoresizingMaskIntoConstraints = false
With Auto Layout, you must not set the frame size yourself.
You are giving the same size to every label you create. The size is not going to change just because you change the text it contains.
Labels have intrinsic size, but when you create labels in code, not the storyboard, you need to set translatesAutoResizingMaskIntoConstraints to false in order for this to work. When you create labels in the storyboard, this is done for you automatically.
For more info, see https://developer.apple.com/reference/uikit/uiview/1622572-translatesautoresizingmaskintoco?language=objc

Setting UITextView height to a variable number of lines

I have a a UITextView populated with a paragraph of text with variable height at runtime.
At runtime, how do I adjust the height of UITextView to the height of a variable number of lines (not necessarily equal to the number lines in the paragraph of text)?
For example:
Set UITextView to the height of 2 lines of text for some cases of text, then
Set UITextView to the height of 5 lines of text for other cases of text (with a scroll-bar to scroll for the other lines as is default behavior)
The analogue in CSS would be to set the view height to 2em if the text is "small", or to 5em if the text is "large", or in Android setting maxLines on a TextView at runtime.
you can use autolayout. first you create a height constraint for your UITextView. then you outlet this constraint to your controller. then you can manipulate height from controller anytime you like.
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
heightConstraint.constant = 200 // or something else
}

How to prevent UILabel to fill the entire screen?

I am trying to display a UILabel that may take up multiple lines but I'm having problem with how the height is resized.
Here is what it looks when I have text over a single line, displaying correctly:
When the text spans multiple lines however this happens:
Here's the interface builder settings I'm using:
Ideally I'd like the text view to remain at athe top of the screen and just take up as much space as it needs to diaplay the text but I really can't tell where I am going wrong.
The text view is a bit tricky to handle with automatic layout. If possible use an UILabel. If not then there are several issues with the text view and the most manageable solution is to add the height constraint which is then manipulated in the code.
The height of the text view content can be determined as:
let height = textView.sizeThatFits(textView.frame.size).height
It is also possible to use
let height = textView.contentSize.height
But the results are sometimes incorrect.
You do need to then set the delegate for the text view so that on change you will refresh the size of the text view.
Well you did give it permission to do so based on your constraints. Any height > 0 as long as it's 20 from the top margin. Since you don't have any other views to base your height off of you can hook up an outlet to your label and use this:
#IBOutlet weak var label: UILabel!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
label.sizeToFit()
}
Uncheck the "Preferred Width" explicit checkbox(In Size Inspector)
Remove the height constraint on you UILabel.
It will definitely work.

Unable to get the height of a UILabel with numberOfLines = 0

When I set a text to my label and check myLabel.frame.size.height I get always the same value, as if being always a single line, even if the text I set takes multiple lines... How could I get the "final" height?
Thanks
Try it inside your View Controller's viewDidLayoutSubviews function:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(myLabel.frame.size.height)
}
Your label won't have it's final size until then.

Resources