How to use sizeThatFits in Swift?
10,990
Try this:
// Get the width you want to fit
let fixedWidth = textView.frame.size.width
// Calculate the biggest size that fixes in the given CGSize
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
// Set the textView's size to be whatever is bigger: The fitted width or the fixedWidth
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
// Make the "background_img_view" height match the textView's height
background_img_view.frame.size.height = textView.frame.size.height
Author by
SIAJSAJ IJSAIJSAJA
Updated on June 04, 2022Comments
-
SIAJSAJ IJSAIJSAJA almost 2 years
I have got a text view and a view like this
let lb = UITextView() let view = UIView() background_img_view.addSubview(about_txt)
lb
doesn't have fixed height,it can be 30 or 300px,how can i usesizeThatFits
to makebackground_img_view
's height depend onlb
's? -
traisjames almost 7 yearsCould you add some comments to each line of code to say what it does?