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;
            }
    
        }
  • 相关阅读:
    概率论基础学习笔记
    树点涂色
    2016北京集训测试赛(八)Problem C: 直径
    BZOJ 4361 ISN
    2017省选集训测试赛(二十五)Problem B recollection
    2016北京集训测试赛(六)Problem B: 矩阵
    记录Vue和Jquery混合开发中关于点击事件的一个bug
    记录JQ-WEUI中滚动加载的一个BUG
    Vue Elementui 如何让输入框每次自动聚焦
    什么是CDN加速?(转载)
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5902743.html
Copyright © 2011-2022 走看看