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

  • 相关阅读:
    html5 canvas雨点打到窗玻璃动画
    html5跟随鼠标炫酷网站引导页动画特效
    如何实现复选框的全选和取消全选效果
    CSS3透明属性opacity
    jQuery实现方式不一样的跳转到底部
    ul li设置横排,并除去li前的圆点
    jQuery美女幻灯相册轮播源代码
    微软modern.IE网站,多版本IE免费测试工具集
    css中position与z-index
    C#一个方法返回多个值
  • 原文地址:https://www.cnblogs.com/codemakerhj/p/4675846.html
Copyright © 2020-2023  润新知