如何限制 UITextView 中的字符?
我一直在寻找解决方案并找到了以下代码。但不幸的是我不知道如何使用它。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 25) ? NO : YES;
}
仅出于测试目的,我设置了 IBACTION
-(IBAction)checkIfCorrectLength:(id)sender{
[self textView:myTextView shouldChangeTextInRange: ?? replacementText: ?? ];
}
我为 shouldChangeTextInRange
和 replacementText
传递什么? 或者我完全错了?
I have been looking for solutions and found the following piece of code. But I do not know how to use it, unfortunately.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 25) ? NO : YES;
}
Just for testing purposes I set up an IBACTION
-(IBAction)checkIfCorrectLength:(id)sender{
[self textView:myTextView shouldChangeTextInRange: ?? replacementText: ?? ];
}
What do I pass for shouldChangeTextInRange
and replacementText
?
Or am I getting it completely wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从
checkIfCorrectLength:
调用textView:shouldChangeTextInRange:replacementText:
没有意义。如果您想通过多种方法测试长度,请将测试分解到其自己的方法中:Calling
textView:shouldChangeTextInRange:replacementText:
fromcheckIfCorrectLength:
doesn't make sense. If you want to test the length from multiple methods, factor the test out into its own method:您好,我在这里找到并修改了代码。所以对于xamarin用户来说。尝试以下操作:
Hi I found and modified the code here. So for xamarin users. try the following:
您不必自己调用此方法,文本视图会在要更改其文本时调用它。只需设置文本视图的
delegate
属性(例如到您的视图控制器)并在那里实现该方法。You don't call this method yourself, the text view calls it whenever it's about to change its text. Just set the text view's
delegate
property (e.g. to your view controller) and implement the method there.如果当前对象是文本视图的委托,那么您可以使用以下代码片段:
这对我有用。
If the current object is the delegate of the text view, then you can use the following snippet:
This worked for me.