Swift-文本视图,可展开并保持在中心位置
我正试图实现我脑海中的一个想法,但我被卡住了Swift-文本视图,可展开并保持在中心位置,swift,textview,constraints,Swift,Textview,Constraints,我正试图实现我脑海中的一个想法,但我被卡住了 我需要一个双向扩展的文本视图:宽高 具有最小、最大宽度和最小高度的 这是以父(滚动)视图的中间为中心的。 在视图的底部尾部有一个按钮send 这是一个想法: func textViewDidChange(_ textView: UITextView) { self.adjustTextViewFrames(textView: textView) } func adjustTextViewFrames(textView : UITextVie
我需要一个双向扩展的文本视图:宽高 具有最小、最大宽度和最小高度的
这是以父(滚动)视图的中间为中心的。
在视图的底部尾部有一个按钮send
这是一个想法:
func textViewDidChange(_ textView: UITextView) {
self.adjustTextViewFrames(textView: textView)
}
func adjustTextViewFrames(textView : UITextView){
var newSize = textView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
if newSize.width > self.view.bounds.width - 20 {
newSize.width = self.view.bounds.width - (self.view.bounds.width/10)
}
messageBubbleTextViewWidthConstraint.constant = newSize.width
messageBubbleTextViewHeightConstraint.constant = newSize.height
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
因此,如果用户在框中键入,则它将在两个方向上展开。但它有一个最大宽度(因此不会脱离屏幕),但高度不受限制:由于父滚动视图
问题在于,当文本插入新行时,文本视图的高度不会扩展。 代码:
func textViewDidChange(_ textView: UITextView) {
self.adjustTextViewFrames(textView: textView)
}
func adjustTextViewFrames(textView : UITextView){
var newSize = textView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
if newSize.width > self.view.bounds.width - 20 {
newSize.width = self.view.bounds.width - (self.view.bounds.width/10)
}
messageBubbleTextViewWidthConstraint.constant = newSize.width
messageBubbleTextViewHeightConstraint.constant = newSize.height
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
试试这个:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let's create our text view
let textView = UITextView()
textView.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
textView.backgroundColor = .lightGray
textView.text = "Here is some default text that we want to show and it might be a couple of lines that are word wrapped"
view.addSubview(textView)
// use auto layout to set my textview frame...kinda
textView.translatesAutoresizingMaskIntoConstraints = false
[
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.heightAnchor.constraint(equalToConstant: 50)
].forEach{ $0.isActive = true }
textView.font = UIFont.preferredFont(forTextStyle: .headline)
textView.delegate = self
textView.isScrollEnabled = false
textViewDidChange(textView)
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: view.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
}
Brian Voong从“让我们在这里构建该应用程序”中受益匪浅您似乎只是真正设置了大小,并没有真正更改
frame.origin.x.
视图在情节提要上以约束为中心,为什么不添加高度约束,设置插座,在调用textView.sizeThatFits()时不断编辑它呢不能将宽度设置为最大有限大小。您需要将宽度设置为所需的实际最大宽度,看起来应该是view.bounds.width-20