Ios 如何将文本视图文本高度限制为10行,然后将剩余文本在Swift3中滚动

Ios 如何将文本视图文本高度限制为10行,然后将剩余文本在Swift3中滚动,ios,swift,swift3,uitextview,Ios,Swift,Swift3,Uitextview,这里我设计了textview,它将随着键盘向上移动,并根据文本调整其高度,在textview中,textview没有滚动,现在我需要textview限制为10行,然后才需要滚动 下面是我的代码: import UIKit class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate{ @IBOutlet weak var msgTextViewheightConstraint: NSLa

这里我设计了textview,它将随着键盘向上移动,并根据文本调整其高度,在textview中,textview没有滚动,现在我需要textview限制为10行,然后才需要滚动

下面是我的代码:

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
    }

}

请帮助我理解这个概念。

您可以通过将
textView
内容的高度除以
textView
字体的线条高度来获得文本的行数:

let numberOfLines = textView.contentSize.height/(textView.font?.lineHeight)!
然后,您可以通过将
textView.contentSize
除以它包含的行数来获得每行的大小:

let lineHeight = textView.contentSize.height / numberOfLines
然后,在您的
viewdide
方法中,您可以将
textView
的高度约束设置为
lineHeight
乘以10:

NSLayoutConstraint.activate([
    textView.heightAnchor.constraint(lessThanOrEqualToConstant: lineHeight * 10)
])
如果执行此操作,请确保未在情节提要中设置高度约束