I need to figure out how to program something like this:
The text expands as the user clicks the down button and fold back to the short version after clicking up. Are there any libraries I could study? I am a beginner in programming and just need advice on how to approach this task, what guides to study and so on.
All you have to do is initially set the UILabel
to have a numberOfLines
of lets say 7 and line break mode to be .byTruncatingTail
.
Then on button click simply change the numberOfLines
to 0 and line break mode to be .byWordWrapping
. Then when you wish to hide the text, just press the button and set the UILabel
to its initial values.
To solve this issue read about AutoLayout
. This is small example how it is possible to do.
This is coding part. This class contain IBOutlet
for the height of the UITextView
and button action.
class ViewController: UIViewController {
let defaultHeight = 128
let expectedHeight = 600
var state: Bool = false
@IBOutlet weak var height: NSLayoutConstraint!
@IBAction func showAction(_ sender: Any) {
UIView.animate(withDuration: 0.3, animations: {
self.state = !self.state
self.height.constant = CGFloat(self.state ? self.expectedHeight: self.defaultHeight)
self.view.layoutIfNeeded()
})
}
}
This is from storyboard.
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