zoukankan      html  css  js  c++  java
  • OC & Swift中UITextFiled、UITextView限制输入字数

    OC中限制字数的方法

    我是用通知实现的,首先添加UITextFiled和UITextView的接收中心

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewNotifitionAction:) name:UITextViewTextDidChangeNotification object:nil];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldNotifitionAction:) name:UITextFieldTextDidChangeNotification object:nil];

    通知调用的方法

    - (void)textViewNotifitionAction:(NSNotification *)userInfo{
    
        if (_textV.text.length>=10) {
            NSString *str = [_textV.text substringToIndex:10];
            _textV.text = str;
        }
    
    }
    
    - (void)textFieldNotifitionAction:(NSNotification *)userInfo{
        if (_textF.text.length>=10) {
            NSString *str = [_textF.text substringToIndex:10];
            _textF.text = str;
        }
    }

    Swift中限制字数的方法

    设置接收中心

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewNotifitionAction:", name: UITextViewTextDidChangeNotification, object: nil); 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFiledNotifitionAction:", name: UITextFieldTextDidChangeNotification, object: nil);

    通知调用的方法

    func textViewNotifitionAction(userInfo:NSNotification){
            let textVStr = textV.text as NSString;
            if (textVStr.length >= 10) {
                let str = textVStr.substringToIndex(10);
                textV.text = str;
            }
    
        }
    func textFiledNotifitionAction(userInfo:NSNotification){
            let textFStr = textF.text! as NSString;
            if (textFStr.length >= 10) {
                let str = textFStr.substringToIndex(10);
                textF.text = str;
            }
    
        }
  • 相关阅读:
    170120、java 如何在pdf中生成表格
    170119、100亿数据1万属性数据架构设计
    170118、快速失败Vs安全失败(Java迭代器附示例)
    170117、spring解决乱码
    170116、centos6.4下nginx和ftp搭建图片服务器
    170113、CentOs6.4中安装和配置vsftp简明教程
    linux nginx完全卸载
    DevOps 的技术栈与工具链
    git与pycharm结合使用
    JMeter和JMeterPlugin的下载安装
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5902743.html
Copyright © 2011-2022 走看看