限制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:
在文本将要更改时调用;您可以使用其文本
属性访问文本视图的当前内容,并且可以从传递的范围和替换文本构建新内容 [ 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屋!