问题描述
我在应用程序中使用UITextView,我希望用户最多输入5行文本,我在Google上待了几天,但仍然没有运气,请有人帮忙!
PS:不限制字符数,但不限制行数"
我尝试查找行数并使用此方法textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
,当达到限制时返回NO,但在达到限制后退格按钮不起作用.
谢谢!
如果没有自动换行,则可以使用以下方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
rows++;
if(rows >= maxNumberOfLines){
//Exit textview
return NO;
}
}
return YES;
}
这应该有效,但这不是处理它的最佳方法.
最好使用字符串的大小并将其与textview的contentsize进行比较以限制它.
I'm using UITextView in my application, i want user to enter maximum 5 lines of text, I've been spending a few days on google but still have no luck, can some one please help!!
PS: Not limit number of characters but number of "LINES"
I've tried finding number of lines and use this method textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
, return NO when limit is reached but the back space button doesn't work after limit reached.
Thanks!
If the lines are not automatically wrapped you could use this:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
rows++;
if(rows >= maxNumberOfLines){
//Exit textview
return NO;
}
}
return YES;
}
This should work, but it's not the best way to deal with it.
It would probably be better to use the size of the string and compare it to the contentsize of the textview to limit it.
这篇关于限制UITextView中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!