zoukankan      html  css  js  c++  java
  • UITextView限制输入字数

    很多时候我们想限制textView中的输入字数,我们可以利用函数- (void)textViewDidChange:(UITextView *)textView中统计textView实现此功能。通过在此函数中统计你输入的字符的个数,当字数超过你限制的字数时调用函数-(NSString *)substringToIndex:(int)length(length是你想限制的字数). 这样当你输入的字符达到限定的个数时,将无法在往textView中输入数据。(实际上是你新输入的数据被函数-(NSString *)substringToIndex:(int)length截掉了。)。还有就是在textView中默认的是一行输入,如果想实现多行输入,需要设 置它的scrollEnabled属性。例如:textView.scrollEnabled = YES;如果想直接让用户不能在textView中编辑,只能阅读 可以设置textView的editable属性。例如textView.editable = YES(可以编辑。下面这行代码实现的是限制textView的字数在128以内。如果超过次数会弹出警告。(其中statusLabel动态的显示 textView中的字符个数)。

    - (void)textViewDidChange:(UITextView *)textView {
        NSInteger number = [textView.text length];
        if (number > 128) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于128" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            textView.text = [textView.text substringToIndex:128];
            number = 128;
            [alert release];
        }
        self.statusLabel.text = [NSString stringWithFormat:@"%d/128",number];
    }

  • 相关阅读:
    openwrt 相关文章
    负载均衡相关文章
    Today's Progress
    Rodrigues formula is beautiful, but uneven to sine and cosine. (zz Berkeley's Page)
    Camera Calibration in detail
    Fundamental Matrix in Epipolar
    Camera Calibration's fx and fy do Cares in SLAM
    FilterEngine::apply
    FilterEngine 类解析——OpenCV图像滤波核心引擎(zz)
    gaussBlur
  • 原文地址:https://www.cnblogs.com/codemakerhj/p/4675846.html
Copyright © 2011-2022 走看看