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.
Related
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.
I'm having this weird issue with UITableView that can't calculate it's content's height properly.
I have custom UITableView class that is embedded in another custom UITableView, I want it to auto-adjust it's height to fit content so I have already:
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
And now when I use:
self.estimatedRowHeight = UITableView.automaticDimension // non-zero value like 40 isn't working either
self.rowHeight = UITableView.automaticDimension
the output is the frame that is not full height, when I turn "Scrolling enabled" in this TableView it's scrollable with full content (don't want that):
Now when I change
self.estimatedRowHeight = UITableView.automaticDimension
to:
self.estimatedRowHeight = 0
the output is exactly what I would want to have except the content text is cut...
Here's my CommentCell:
Console isn't showing any errors with autolayout in any case.
Do you maybe know what's going on? I have spent literally days trying to get those comments to work and that's the last thing I need.
If you need any more info please just tell me.
Edit:
If i change estimatedRowHeight to a large number for example 500 I get loads of empty space under cells:
So it looks like TableView can't fix the cell height to content. Maybe this will help someone.
Maybe it's about the textfield inside the CellView. Did you set it's Layout to wraps?
Also I would try to set it's intrinsic size value to 'placeholder' inside the Size Inspector.
I've put a UILabel in my notification content extension storyboard and set it up using auto layout. I've also set the numberOfLines to 0. In the storyboard, it looks perfectly fine but when I use it on my device, the label is coming out to be a single line. Am I missing anything?
Edit (ScreenShots added):
Also, when a new notification comes, I see a blank info and it's updated after a few secs.
More ScreenShots Added:
Number of lines is set to 0.
and in the code I'm setting it like this:
titleString.text = "Saturday SuperBowl Saturday SuperBowl Saturday SuperBowl"
But still it is coming as a single line.
You must set the uilabels preferredMaxLayoutWidth. You can do this in your view controller:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
titleString.preferredMaxLayoutWidth = titleString.frame.size.width
self.view.layoutIfNeeded()
}
or subclassing your uilabel:
override func layoutSubviews() {
self.preferredMaxLayoutWidth = self.frame.size.width
super.layoutSubviews
}
I have a label in a View Controller scene inside a root view.
I set the constraint of the height to 30 using the Pins button right below.
Everything looks pretty in the storyboard.
But when I want to get the size of the element inside my code I always get the value 21. I tried refLabel.bounds.size.height and refLabel.frame.height. I call this in the override func viewDidLoad(){
super.viewDidLoad() function of the corresponding class.
Has this something to do with the intrinsic content size?
Try this code
refLabel.layoutIfNeeded()
let height = refLabel.frame.size.height
print("right height is \(height)")
When the DTAttributedTextView's frame changes the content (text) inside the view is not relayouted. I have the text view in my tableView cell and its height is specified by a layout constraint, which value I change in code. Width changes automatically with the constraints based on the cell width.
I have tried to manually call setNeedsLayout, layoutIfNeeded, relayoutText, setNeedsDisplay. Nothing seems to help. Why is the content layout locked this way?
This seems to work for the height, but not for the width:
override func layoutSubviews() {
super.layoutSubviews()
comment.attributedTextContentView.layoutFrame = DTCoreTextLayoutFrame(
frame: comment.frame,
layouter: comment.attributedTextContentView.layouter)
comment.relayoutText()
}
Its necessary to set the layouter to nil before calling relayoutText
comment.attributedTextContentView.layoutFrame = DTCoreTextLayoutFrame(
frame: comment.frame,
layouter: comment.attributedTextContentView.layouter)
//gets rid of cached layouter
comment.layouter = nil
comment.relayoutText()