0

キーボードからすべての文字を入力するときに空白を追加しようとしていますが、テキストを削除、コピー、または編集できないという問題があります。

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

    NSUInteger length = [textField.text length] + [string length] - range.length;

    if ([textField tag] == textTag && newLength <= 10) {
        textField.text = [textField.text stringByAppendingString:@" "];
        length ++;
        NSLog(@"%d", length);
    } else {
        NSLog(@"%d", length);
        return NO;
    }

    return YES;

}
4

2 に答える 2

1

こんなこともできる..

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

   NSUInteger length = [textField.text length] + [string length] - range.length;

   if (length < 10) {

        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

        NSString *lastString = [NSString stringWithFormat:@"%c",[newString characterAtIndex:newString.length-1]];

       if(![lastString isEqualToString:@" "] && ![string isEqualToString:@""])
           newString = [newString stringByAppendingString:@" "];
       else {

           NSRange tempR = NSMakeRange(range.location-1, 2);

           newString = [textField.text stringByReplacingCharactersInRange:tempR withString:@""];
       }

       textField.text = newString;
   }

    return NO;
}
于 2013-11-04T12:00:23.523 に答える