-1
這裏我設計了textview,它將隨鍵盤向上移動並根據文本調整其高度,在textview中不會滾動textview ,現在我需要textview應該限制10行然後想要滾動。如何使用10行限制textview文本高度,然後使用剩餘文本在Swift3中滾動
下面是我的代碼:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate{
@IBOutlet weak var msgTextViewheightConstraint: NSLayoutConstraint!
@IBOutlet weak var msgTextView: UITextView!
@IBOutlet weak var textViewBottomLayoutContraint: NSLayoutConstraint!
var activeTextView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
msgTextView.layer.cornerRadius = 5
msgTextView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
msgTextView.layer.borderWidth = 0.5
msgTextView.clipsToBounds = true
self.setupTextView()
self.registerForKeyboardNotifications()
}
func registerForKeyboardNotifications(){
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textViewBottomLayoutContraint.constant = keyboardSize.size.height + 10
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textViewBottomLayoutContraint.constant = 10
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
activeTextView = textView
}
func textViewDidEndEditing(_ textView: UITextView) {
activeTextView = nil
}
private func textViewShouldReturn(_ textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}
func setupTextView() {
if let txtView = msgTextView {
txtView.scrollsToTop = true
txtView.isScrollEnabled = true
txtView.addObserver(self, forKeyPath: "contentSize", options:[ NSKeyValueObservingOptions.old , NSKeyValueObservingOptions.new], context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let changeDict = change, let constraint = msgTextViewheightConstraint, let view = self.msgTextView {
if object as? NSObject == self.msgTextView && keyPath == "contentSize" {
if let oldContentSize = (changeDict[NSKeyValueChangeKey.oldKey] as AnyObject).cgSizeValue,
let newContentSize = (changeDict[NSKeyValueChangeKey.newKey] as AnyObject).cgSizeValue {
let dy = newContentSize.height - oldContentSize.height
constraint.constant = constraint.constant + dy;
self.view.layoutIfNeeded()
let contentOffsetToShowLastLine = CGPoint(x: 0.0, y: view.contentSize.height - view.bounds.height)
view.contentOffset = contentOffsetToShowLastLine
}
}
}
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return true
}
}
請幫我在這個概念。