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];
    }

  • 相关阅读:
    「联赛模拟测试33」题解
    分享几个基于vue的移动端框架
    11-15
    test
    联赛模拟测试20 C. Weed
    联赛模拟测试20 D. Drink
    联赛模拟测试24 联合权值·改
    联赛模拟测试21 表格
    近期的一些考试题目
    shell脚本执行错误 $' ':command not found
  • 原文地址:https://www.cnblogs.com/codemakerhj/p/4675846.html
Copyright © 2011-2022 走看看