[英]How to create an expandable text field/label in swift?
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
. 你所要做的就是最初将
UILabel
设置为numberOfLines
,即7和断行模式为.byTruncatingTail
。
Then on button click simply change the numberOfLines
to 0 and line break mode to be .byWordWrapping
. 然后在按钮上单击,只需将
numberOfLines
更改为0,将换行模式更改为.byWordWrapping
。 Then when you wish to hide the text, just press the button and set the UILabel
to its initial values. 然后,当您想要隐藏文本时,只需按下按钮并将
UILabel
设置为其初始值。
To solve this issue read about AutoLayout
. 要解决此问题,请阅读有关
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. 此类包含
IBOutlet
用于UITextView
和按钮操作的高度。
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. 这是来自故事板。
You can set an height constraint for your UILabel, and on the tap button event change the constraint constant to the contentsize of your label. 您可以为UILabel设置高度约束,并在点击按钮事件上将约束常量更改为标签的内容大小。
After your can animate it with 之后你可以用它制作动画
UIView.animate(duration)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.