ios - 文本跳转,同时在 swift 中自动调整 textView 的大小
<p><p>如果我的应用中有一个简单的 messageView。 messageView 上是一个带有 textView 的输入容器。 textView 应根据其内容调整大小。</p>
<p>到目前为止它可以工作,但是每次换行到下一行时,文本都会“跳转”第一个字符,但会用第二个字符重新定位。看起来像:
<a href="/image/Vy82j.png" rel="noreferrer noopener nofollow"><img src="/image/Vy82j.png" alt="enter image description here"/></a> </p>
<p> <a href="/image/o9bMz.png" rel="noreferrer noopener nofollow"><img src="/image/o9bMz.png" alt="enter image description here"/></a> </p>
<p> <a href="/image/htaQi.png" rel="noreferrer noopener nofollow"><img src="/image/htaQi.png" alt="enter image description here"/></a> </p>
<p>我的大部分代码。我认为它与 textView(?) 的滚动功能有关。</p>
<pre><code>private let container: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 20
view.layer.masksToBounds = true
view.layer.borderColor = UIColor(red:0.90, green:0.90, blue:0.90, alpha:1.0).cgColor
view.layer.borderWidth = 0.5
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private lazy var inputTV: UITextView = {
let tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.font = UIFont(name: "OpenSans-Light", size: 16)
tv.backgroundColor = .red
tv.delegate = self
tv.textContainer.lineBreakMode = .byWordWrapping
return tv
}()
override internal init(frame: CGRect) {
super.init(frame: CGRect.zero)
translatesAutoresizingMaskIntoConstraints = false
addSubview(container)
container.addSubview(inputTV)
container.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16).isActive = true
container.leftAnchor.constraint(equalTo: leftAnchor , constant: 16).isActive = true
container.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
containerHeightAnchor = container.heightAnchor.constraint(equalToConstant: 40)
containerHeightAnchor?.isActive = true
inputTV.leftAnchor.constraint(equalTo: uploadButton.rightAnchor).isActive = true
inputTV.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
inputTV.rightAnchor.constraint(equalTo: sendButton.leftAnchor, constant: -5).isActive = true
textViewHeightAnchor = inputTV.heightAnchor.constraint(equalTo: container.heightAnchor)
textViewHeightAnchor?.isActive = true
}
internal func textViewDidChange(_ textView: UITextView) {
let contentHeight = textView.contentSize.height
containerHeightAnchor?.constant = max(contentHeight, 40)
inputTV.frame.size.height = contentHeight
}
</code></pre>
<p>我希望有人可以提供帮助。问候</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>感谢@DonMag,我确实从 <code>textView</code> 中删除了 <code>heightAnchor</code>,它确实对我有用。新代码如下:</p>
<ul>
<li><p>在 textView 上禁用滚动 (<code>isScrollEnabled = false</code>)</p>
<pre><code>private lazy var inputTV: UITextView = {
let tv = UITextView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.font = UIFont(name: "OpenSans-Light", size: 16)
tv.delegate = self
tv.isScrollEnabled = false
return tv
}()
</code></pre> </li>
<li><p>删除 heightAnchor(<strong>no</strong> <code>inputTV.heightAnchor</code> 分别是我的 <code>textViewHeightAnchor</code>)</p>
<pre><code>override internal init(frame: CGRect) {
super.init(frame: CGRect.zero)
translatesAutoresizingMaskIntoConstraints = false
addSubview(container)
container.addSubview(inputTV)
...
inputTV.leftAnchor.constraint(equalTo: uploadButton.rightAnchor).isActive = true
inputTV.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
inputTV.rightAnchor.constraint(equalTo: sendButton.leftAnchor, constant: -5).isActive = true
}
</code></pre> </li>
</ul></p>
<p style="font-size: 20px;">关于ios - 文本跳转,同时在 swift 中自动调整 textView 的大小,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/51046186/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/51046186/
</a>
</p>
页:
[1]