如何迅速避免出现太多新行

问题描述 投票:-1回答:1

我将文本视图用于用户的内容输入,但是我不能限制用户点击以创建新行的次数,如何将textView限制为最多2个新行?

示例:

当前

"Start





end"

所需:

"limit


2 


empty lines"
ios swift textview
1个回答
0
投票

您可以使用将视图控制器作为文本视图的委托,并使用replaceOccurrences用2条新行替换3条或更多条新行:

class ViewController: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
    }
    func textViewDidChange(_ textView: UITextView) {
        textView.text = textView.text.replacingOccurrences(of: "\n{3,}", with: "\n\n", options: .regularExpression)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.