限制 UITextview 的行数 [英] Limit the number of lines for UITextview
问题描述
我想知道如何限制用户在编辑 UITextField 时可以输入的 LINES(不是其他问题中询问的字符)的数量.
I was wondering how to limit the amount of LINES (not characters as asked in other questions) a user can enter when editing a UITextField.
理想情况下,我想将输入限制为最大值.10 行.
Ideally, I would like to limit the input to max. 10 lines.
我需要从哪里开始?我用一种方法来做到这一点吗?在
Where would I need to start? Do I do this with a method? In
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView
推荐答案
你的想法是对的,但方法是错误的.textView:shouldChangeTextInRange:replacementText:
每当文本要改变时调用;您可以使用其 text
属性访问文本视图的当前内容,并且您可以使用 [textView.text stringByReplacingCharactersInRange:range withString:replacementText 从传递的范围和替换文本中构造新内容]代码>.然后,您可以计算行数并返回 YES 以允许更改或 NO 拒绝更改.
You have the right idea, but the wrong method. textView:shouldChangeTextInRange:replacementText:
is called whenever the text is going to change; you can access the current content of the text view using its text
property, and you can construct the new content from the passed range and replacement text with [textView.text stringByReplacingCharactersInRange:range withString:replacementText]
. You can then count the number of lines and return YES to allow the change or NO to reject it.
这篇关于限制 UITextview 的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!