I've already set the properties textContainerInset
and lineFragmentPadding
to zero as seen on this code thanks to this removing the padding and margin via this SO answer.
// this is inside a UITextView Subclass
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.textContainerInset = .zero
self.textContainer.lineFragmentPadding = 0
}
Here's how the UITextView
renders when it has text
Here it is with multiple lines
Here's how it renders without text
Is it possible to make the height 0
if the UITextView
's text is empty?
Edit:
There is no constraint being used on the UITextView
and I'm not planning to set a height constraint as I want this UITextView
to automatically resize depending on the text being set into it
This is a isScrollEnabled = false
UITextView inside a UITableViewCell, which automatically resizes depending on the data fetched from the api server.
About:
Language: Swift 3.2
IDE: Xcode 9.2
Thanks to uhuru's answer I've formulated an answer to the code of mine that wouldn't need too much of an overhaul.
First I've setup a height constraint for the UITextView
programatically.
// outside the scope
var contentTextViewConstraint: NSLayoutConstraint?
// inside awakeFromNib
self.contentTextViewConstraint = NSLayoutConstraint(
item: self.lblContent,
attribute: NSLayoutAttribute.height,
relatedBy: NSLayoutRelation.equal,
toItem: nil,
attribute: NSLayoutAttribute.notAnAttribute,
multiplier: 1,
constant: 0)
self.contentTextViewConstraint?.isActive = false
Then activate/deactivate the constraint depending on the String
// inside the setup
let contentText: String = model.contentText
self.tvContent.text = contentText
self.contentTextViewConstraint?.isActive = contentText.isEmpty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With