1

我希望 UItextView 在 TextView.contentSize.height 超过一定限制后不再接受任何字符。但可以删除内容。如何实现这一点。有什么建议吗??

4

3 回答 3

0

使用文本字段和文本视图的委托和数据源。

下面是文本字段委托方法的示例

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if (textField == self.txtContact) {
            if ([DebugLog isEqualToString:@"1"]) {
                NSLog(@"%@",textField.text);
            }
            char *x = (char*)[string UTF8String];
            if([string isEqualToString:@"-"] || [string isEqualToString:@"("] || [string isEqualToString:@")"] || [string isEqualToString:@"0"] || [string isEqualToString:@"1"] ||  [string isEqualToString:@"2"] ||  [string isEqualToString:@"3"] ||  [string isEqualToString:@"4"] ||  [string isEqualToString:@"5"] ||  [string isEqualToString:@"6"] ||  [string isEqualToString:@"7"] ||  [string isEqualToString:@"8"] ||  [string isEqualToString:@"9"] || x[0]==0 || [string isEqualToString:@" "]) {
                    if ([DebugLog isEqualToString:@"1"]) {
                        NSLog(@"%d",[string length]);
                    }
                NSUInteger newLength = [textField.text length] + [string length];
                if (!CheckDigit) {
                    if ([textField.text length] == 4) {
                        textField.text =[NSString stringWithFormat:@"%@-",textField.text];
                        CheckDigit=TRUE;
                    }


                }
                if (!CheckDigit1) {
                    if ([textField.text length] == 9) {
                        textField.text =[NSString stringWithFormat:@"%@-",textField.text];
                        CheckDigit1=TRUE;
                    }


                }
                if ([textField.text length]<=4 ) {
                    CheckDigit=FALSE;
                }
                if ([textField.text length]<=9 && [textField.text length]>=4 ) {
                    CheckDigit1=FALSE;
                }

                return (newLength >  11) ? NO : YES;

            } else {
                return NO;
            }

}
于 2012-08-22T12:15:56.743 回答
0

看看UITextFieldDelegate协议。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string是你的朋友。

于 2012-08-22T12:16:05.193 回答
0

用户关注委托方法以避免用户输入任何内容

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

   return 0;/// Dont allow user to type

return 1; ///Allow user to type

  }
于 2012-08-22T12:18:36.527 回答