TextView不包装

时间:2018-01-05 21:05:14

标签: ios swift uitextview

我有一个textview而不是textfield,所以我可以自动换行文本。但是,文本本身不是自动换行。当行太长时,以下文本就会消失。如果我转到该行中的较早点并开始删除文本,则开始显示不可见的文本。它应该自动自动化但不会。

import UIKit

protocol CommentInputAccessoryViewDelegate {
    func handleSubmit(for comment: String?)
}

class CommentInputAccessoryView: UIView, UITextViewDelegate {
    var delegate: CommentInputAccessoryViewDelegate?
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

   fileprivate let submitButton: UIButton = {
        let submitButton = UIButton(type: .system)
        submitButton.setTitle("Submit", for: .normal)
        submitButton.setTitleColor(.black, for: .normal)
        submitButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        submitButton.addTarget(self, action: #selector(handleSubmit), for: .touchUpInside)
        //submitButton.isEnabled = false
        return submitButton
    }()

    lazy var commentTextView: UITextView = {
        let textView = UITextView()
       // textView.placeholder = "Add a comment"
        textView.delegate = self
        textView.isScrollEnabled = false
        textView.backgroundColor = .red
        textView.font = UIFont.boldSystemFont(ofSize: 12)
        textView.textContainer.lineBreakMode = .byWordWrapping
       // textView.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
       // backgroundColor = .red
        //1
        autoresizingMask = .flexibleHeight
        addSubview(submitButton)
        submitButton.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right:rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 50, height: 0)
        addSubview(commentTextView)
        //3
        if #available(iOS 11.0, *){
                    commentTextView.anchor(top: topAnchor, left: leftAnchor, bottom: safeAreaLayoutGuide.bottomAnchor, right: submitButton.leftAnchor, paddingTop: 0, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        }else{
            //fallback on earlier versions
        }

        setupLineSeparatorView()

    }
    //2
    override var intrinsicContentSize: CGSize{
        return .zero
    }

    fileprivate func setupLineSeparatorView(){
        let lineSeparatorView = UIView()
        lineSeparatorView.backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
        addSubview(lineSeparatorView)
        lineSeparatorView.anchor(top:topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0.5)
    }

    @objc func handleSubmit(){
        guard let commentText = commentTextView.text else{
            return
        }
        delegate?.handleSubmit(for: commentText)
    }

    @objc func textFieldDidChange(_ textField: UITextView) {
        let isCommentValid = commentTextView.text?.count ?? 0 > 0
        if isCommentValid {
            submitButton.isEnabled = true
        }else{
            submitButton.isEnabled = false
        }
    }
    func clearCommentTextField(){
        commentTextView.text = nil
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

我认为这是主力,但我并不完全确定。

0 个答案:

没有答案