如何限制 UITextView 中的字符?

发布于 2025-01-02 16:23:41 字数 635 浏览 0 评论 0原文

我一直在寻找解决方案并找到了以下代码。但不幸的是我不知道如何使用它。

- (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: ?? ];

}

我为 shouldChangeTextInRangereplacementText 传递什么? 或者我完全错了?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉态萌生 2025-01-09 16:23:41

checkIfCorrectLength: 调用 textView:shouldChangeTextInRange:replacementText: 没有意义。如果您想通过多种方法测试长度,请将测试分解到其自己的方法中:

- (BOOL)isAcceptableTextLength:(NSUInteger)length {
    return length <= 25;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
    return [self isAcceptableTextLength:textField.text.length + string.length - range.length];
}

-(IBAction)checkIfCorrectLength:(id)sender{
    if (![self isAcceptableTextLength:self.textField.text.length]) {
        // do something to make text shorter
    }
}

Calling textView:shouldChangeTextInRange:replacementText: from checkIfCorrectLength: doesn't make sense. If you want to test the length from multiple methods, factor the test out into its own method:

- (BOOL)isAcceptableTextLength:(NSUInteger)length {
    return length <= 25;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
    return [self isAcceptableTextLength:textField.text.length + string.length - range.length];
}

-(IBAction)checkIfCorrectLength:(id)sender{
    if (![self isAcceptableTextLength:self.textField.text.length]) {
        // do something to make text shorter
    }
}
杀手六號 2025-01-09 16:23:41

您好,我在这里找到并修改了代码。所以对于xamarin用户来说。尝试以下操作:

textView.ShouldChangeText += delegate
{
   if(textView.Text.Length > 159) // limit to one sms length
   {
      return false;
   }

   return true;
}

Hi I found and modified the code here. So for xamarin users. try the following:

textView.ShouldChangeText += delegate
{
   if(textView.Text.Length > 159) // limit to one sms length
   {
      return false;
   }

   return true;
}
北方的韩爷 2025-01-09 16:23:41

您不必自己调用此方法,文本视图会在要更改其文本时调用它。只需设置文本视图的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.

时光暖心i 2025-01-09 16:23:41

如果当前对象是文本视图的委托,那么您可以使用以下代码片段:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return weightTextView.text.length + text.length - range.length < 7;
}

这对我有用。

If the current object is the delegate of the text view, then you can use the following snippet:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return weightTextView.text.length + text.length - range.length < 7;
}

This worked for me.

时光无声 2025-01-09 16:23:41
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text length] == 0)
{
    if([textView.text length] != 0)
    {
        return YES;
    }
    else {
        return NO;
    }
}
else if([[textView text] length] > your limit value ) 
{
    return NO;
}
return YES;

}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text length] == 0)
{
    if([textView.text length] != 0)
    {
        return YES;
    }
    else {
        return NO;
    }
}
else if([[textView text] length] > your limit value ) 
{
    return NO;
}
return YES;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文