gpt4 book ai didi

ios - 当文本下降到 Swift 中的键盘级别时,如何在我的 TextView 中实现自动滚动?

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:15 28 4
gpt4 key购买 nike

我知道当内容(文本)超过 TextView 的默认高度时会自动滚动,但我希望 TextView 是全屏的,当内容到达键盘级别时, TextView 应该滚动自动地。我已经给出了一个 GIF 以帮助读者更好地理解。

Notes on IOS

class StartStoryPopUp: UIViewController, UITextViewDelegate {
// Setup text view
func setupTextView() {

textViewOne.textColor = .black
textViewOne.backgroundColor = baseColor
// Add some padding to the text insde the text view
textViewOne.textContainerInset = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
textViewOne.font = UIFont(name: "PatrickHand-Regular", size: 25)
textViewOne.layer.cornerRadius = 25

popUp.addSubview(textViewOne)
addTextViewConstraints()
}

// Add the constraints to the 'start story' text view
func addTextViewConstraints() {

textViewOne.translatesAutoresizingMaskIntoConstraints = false
textViewOne.leadingAnchor.constraint(equalTo: popUp.leadingAnchor, constant: 3).isActive = true
textViewOne.trailingAnchor.constraint(equalTo: popUp.trailingAnchor, constant: -3).isActive = true
textViewOne.topAnchor.constraint(equalTo: popUp.topAnchor, constant: 3).isActive = true
textViewOne.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive = true
}
}

最佳答案

swift 5

也许这条线对你有用。

您应该在 textViewDidChange 函数中设置 contentOffSet,如下所示:

func textViewDidChange(_ textView: UITextView) {

let line = textView.caretRect(for: (textView.selectedTextRange?.start)!)
let overFlow = line.origin.y + line.size.height - (textView.contentOffset.y + textView.bounds.size.height - textView.contentInset.bottom - textView.contentInset.top)

if 0 < overFlow {

var offSet = textView.contentOffset
offSet.y += (overFlow + 7)

UIView.animate(withDuration: 0.3, animations: {
textView.setContentOffset(offSet, animated: true)
})
}
}

关于ios - 当文本下降到 Swift 中的键盘级别时,如何在我的 TextView 中实现自动滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747257/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com