Swift中TextField

 


       

         // 判断文本框 是否为空
        if IDTextField?.text?.isEmpty == true {
            
            JRToast.showWithText("请输入帐号", duration: 1.5);
 
            return;
            
        }
swift

 


//
//  UITextField+Extension.swift
//  EasyHome
//
//  Created by codeIsMyGirl on 16/5/5.
//  Copyright © 2016年 codeIsMyGirl. All rights reserved.
//

import Foundation

/*
 
 这是一个最顶级的类
 
 对指定类 进行构造方法的扩展
 
 convenience 表示 遍历构造函数

 */

import Foundation

// 对 UITextField 进行构造方法扩展

extension UITextField {
    
    /// 无占位图
    convenience init(stringPlaceholder: String) {
        
        self.init();
        
        // 提示默认提示符字符
        placeholder = stringPlaceholder;
        
        // 设置边框属性
        borderStyle = .RoundedRect;
        
        // 清除方式
        clearButtonMode = .WhileEditing;
        
        /*
         一直显示
         Always
         
         当前焦距内 显示
         WhileEditing
         
         从不显示
         Never
         
         焦距 不在当前 textField 显示
         UnlessEditing
         
         */
    }

    /// 有占位图
    convenience init(imageView: UIImageView, string: String, isSecure : Bool) {

        self.init();
        
        // 左侧占位图
        leftView = imageView;
        
        // 老是显示 imageView 的样式
        leftViewMode = .Always;
        
        // 提示默认提示符字符
        placeholder = string;
        
        // 设置边框属性
        borderStyle = .RoundedRect;
        
        /*
         
         代码建立 默认 无边框
         
         虚线 边框
         RoundedRect
         
         
         */

        // 打开 暗文显示
        secureTextEntry = isSecure;
        
        // 清除方式
        clearButtonMode = .WhileEditing;
    }
}
less

限定文本框输入长度

// 添加事件
        <#textField#>.addTarget(self, action: "inputTextFiledTolengthChange:", forControlEvents: .EditingChanged);

    /// 上一次的次数
    private var countString = "0";

    // MARK:
    // MARK: 输入了文本框
    /// 输入了文本框
    @objc private func inputTextFiledTolengthChange(sender: UITextField) {
 
        if sender.text!.characters.count >= 5 {
            
            sender.text = countString;
            
            return;
        }
        
        countString = sender.text ?? "0";
 
    }

参考:http://www.jianshu.com/p/0ba0395f4060
相关文章
相关标签/搜索