我将文本视图用于用户的内容输入,但是我不能限制用户点击以创建新行的次数,如何将textView限制为最多2个新行?
示例:
当前
"Start
end"
所需:
"limit
2
empty lines"
您可以使用将视图控制器作为文本视图的委托,并使用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)
}
}