限制UITextView或UITextField中的粘贴字符串长度

时间:2009-07-16 06:22:15

标签: iphone cocoa-touch iphone-sdk-3.0 uitextfield uitextview

限制直接输入UITextView或UITextField的字符串的问题已在SO上解决:

然而现在使用OS 3.0复制和粘贴成为一个问题,因为上述SO问题中的解决方案不会阻止粘贴其他字符(即,您不能在上面配置的字段中键入超过10个字符解决方案,但您可以轻松地将100个字符粘贴到同一个字段中。)

有没有办法防止直接输入字符串粘贴字符串溢出?

7 个答案:

答案 0 :(得分:10)

我能够通过符合UITextViewDelegate协议中的textViewDidChange:方法来限制输入和粘贴的文本。

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length >= 10)
    {
        textView.text = [textView.text substringToIndex:10];
    }
}

但我仍然认为这种丑陋的黑客攻击,似乎Apple应该提供UITextFields和UITextViews的某种“maxLength”属性。

如果有人知道更好的解决方案,请告诉我们。

答案 1 :(得分:7)

根据我的经验,只需实现委托方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

适用于粘贴。整个粘贴的字符串在replacementString:参数中出现。只检查它的长度,如果它长于你的最大长度,那么只需从这个委托方法返回NO。这导致无法粘贴任何内容。或者你可以像之前的答案建议那样对它进行子串,但如果它太长,这可以防止粘贴,如果这是你想要的。

答案 2 :(得分:6)

在textViewDidChange中插入文本后更改文本:如果用户按下“撤消”,则会导致应用崩溃。粘贴后。

我玩了很多,并且能够得到一个有效的解决方案。基本上逻辑是,如果总长度大于最大字符,则不允许粘贴,检测溢出的数量并仅插入部分字符串。

使用此解决方案,您的粘贴板和撤消管理器将按预期工作。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSInteger newLength = textView.text.length - range.length + text.length;

    if (newLength > MAX_LENGTH) {
        NSInteger overflow = newLength - MAX_LENGTH;

        dispatch_async(dispatch_get_main_queue(), ^{
            UITextPosition *start = [textView positionFromPosition:nil offset:range.location];
            UITextPosition *end = [textView positionFromPosition:nil offset:NSMaxRange(range)];
            UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
            [textView replaceRange:textRange withText:[text substringToIndex:text.length - overflow]];
        });
        return NO;
    }
    return YES;
}

答案 3 :(得分:0)

此外,'[string length]'中的字符串长度是一回事,但通常需要截断到某个编码中的字节数。我需要截断键入并粘贴到UITextView中以达到最大UTF8计数,这就是我如何做到的。 (为UITextField做类似的事情对读者来说是一种练习。)

的NSString + TruncateUTF8.h

#import <Foundation/Foundation.h>
@interface NSString (TruncateUTF8)
- (NSString *)stringTruncatedToMaxUTF8ByteCount:(NSUInteger)maxCount;
@end

的NSString + TruncateUTF8.m

#import "NSString+TruncateUTF8.h"
@implementation NSString (TruncateUTF8)
- (NSString *)stringTruncatedToMaxUTF8ByteCount:(NSUInteger)maxCount {
  NSRange truncatedRange = (NSRange){0, MIN(maxCount, self.length)};
  NSInteger byteCount;

  // subtract from this range to account for the difference between NSString's
  // length and the string byte count in utf8 encoding
  do {
    NSString *truncatedText = [self substringWithRange:truncatedRange];
    byteCount = [truncatedText lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    if (byteCount > maxCount) {
      // what do we subtract from the length to account for this excess count?
      // not the count itself, because the length isn't in bytes but utf16 units
      // one of which might correspond to 4 utf8 bytes (i think)
      NSUInteger excess = byteCount - maxCount;
      truncatedRange.length -= ceil(excess / 4.0);
      continue;
    }
  } while (byteCount > maxCount);

  // subtract more from this range so it ends at a grapheme cluster boundary
  for (; truncatedRange.length > 0; truncatedRange.length -= 1) {
    NSRange revisedRange = [self rangeOfComposedCharacterSequencesForRange:truncatedRange];
    if (revisedRange.length == truncatedRange.length)
      break;
  }

  return (truncatedRange.length < self.length) ? [self substringWithRange:truncatedRange] : self;
}
@end

// tested using:
//    NSString *utf8TestString = @"Hello world, Καλημέρα κόσμε, コンニチハ ∀x∈ℝ ıntəˈnæʃənəl ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ STARGΛ̊TE γνωρίζω გთხოვთ Зарегистрируйтесь ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช ሰማይ አይታረስ ንጉሥ አይከሰስ። ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ⡌⠁⠧⠑ ⠼⠁⠒  ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ ░░▒▒▓▓██ ▁▂▃▄▅▆▇█";
//    NSString *truncatedString;
//    NSUInteger byteCount = [utf8TestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"length %d: %p %@", (int)byteCount, utf8TestString, utf8TestString);
//    for (; byteCount > 0; --byteCount) {
//        truncatedString = [utf8TestString stringTruncatedToMaxUTF8ByteCount:byteCount];
//        NSLog(@"truncate to length %d: %p %@ (%d)", (int)byteCount, truncatedString, truncatedString, (int)[truncatedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
//    }

MyViewController.m

#import "NSString+TruncateUTF8.h"
...
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)replacementText
{
  NSMutableString *newText = textView.text.mutableCopy;
  [newText replaceCharactersInRange:range withString:replacementText];

  // if making string larger then potentially reject
  NSUInteger replacementTextLength = replacementText.length;
  if (self.maxByteCount > 0 && replacementTextLength > range.length) {
    // reject if too long and adding just 1 character
    if (replacementTextLength == 1 && [newText lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > self.maxByteCount) {
      return NO;
    }

    // if adding multiple charaters, ie. pasting, don't reject altogether but instead return YES
    // to accept and truncate immediately after, see http://stackoverflow.com/a/23155325/592739
    if (replacementTextLength > 1) {
      NSString *truncatedText = [newText stringTruncatedToMaxUTF8ByteCount:self.maxByteCount]; // returns same string if truncation needed
      if (truncatedText != newText) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0LL), dispatch_get_main_queue(), ^{
          UITextPosition *replaceStart = [textView positionFromPosition:textView.beginningOfDocument offset:range.location];
          UITextRange *textRange = [textView textRangeFromPosition:replaceStart toPosition:textView.endOfDocument];
          [textView replaceRange:textRange withText:[truncatedText substringFromIndex:range.location]];

          self.rowDescriptor.value = (truncatedText.length > 0) ? truncatedText : nil;
        });
      }
    }
  }

  [self updatedFieldWithString:(newText.length > 0) ? newText : nil]; // my method
  return YES;
}

答案 4 :(得分:0)

如果您在string.length委托方法

中检查shouldChangeCharactersIn range:,则可以知道粘贴的字符串
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.length > 1 {
        //pasted string
        // do you stuff like trim
    } else {
        //typed string
    }
    return true
}

答案 5 :(得分:0)

此代码不允许用户输入比maxCharacters更多的字符。 如果粘贴的文本超出此限制,粘贴命令将不执行任何操作。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
    return newText.count <= maxCharacters;
}

答案 6 :(得分:-1)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

  if(string.length>10){
    return NO;
  }
  return YES;
}