swift UITextField

来源:互联网 发布:惠斯通电桥测电阻数据 编辑:程序博客网 时间:2024/05/16 05:08

 /* swift 3.0    UITextField的使用  */

        

        /*

        1. 创建UITextField

        2. 设置边框样式  borderStyle

            .none         无边框

            .line         直线边框

            .roundedRect  圆角矩形边框

            .bezel        边线+阴影

            

         不过可以使用

                 textField.layer.borderWidth = 3;

                 textField.layer.borderColor = UIColor.red.cgColor

                 textField.layer.masksToBounds = true

                 textField.layer.cornerRadius = 5;

            来实现同样的效果

         */

        let textField:UITextField = UITextField()

        textField.borderStyle = .none

        textField.borderStyle = .line

        textField.borderStyle = .bezel

        textField.borderStyle = .roundedRect

        

        textField.bounds =CGRect.init(origin:CGPoint.zero, size:CGSize.init(width:100, height: 40))

        textField.center =view.center

//        textField.text = "1111111"

        textField.textColor =UIColor.red

        textField.backgroundColor =UIColor.yellow

        textField.textAlignment = .center

        view.addSubview(textField)

        

        

        

        

        

        /* 2. 文本提示文字   */

        textField.placeholder ="请输入用户名字"

        

        

        

        

        

        /* 3. 文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号 */

        textField.adjustsFontSizeToFitWidth =true

        textField.minimumFontSize =20

        

        

        

        

        /* 4. 水平,垂直对齐方式 */

        /* 水平对齐 */

        textField.textAlignment = .right/* 水平右对齐 */

        textField.textAlignment = .center/* 水平居中对齐 */

        textField.textAlignment = .left  /* 水平左对齐 */

        /* 垂直对齐 */

        textField.contentVerticalAlignment = .top/* 垂直向上对齐 */

        textField.contentVerticalAlignment = .center/* 垂直居中对齐 */

        textField.contentVerticalAlignment = .bottom/* 垂直向下对齐 */

        

        

        

        /* 5. 背景图片设置 */

        textField.borderStyle = .none /* 先要去除边框样式 */

        textField.background =UIImage.init(named:"a.png")

        

        

        

        /* 6. 清除按钮(输入框内右侧小叉) */

        textField.clearButtonMode = .whileEditing/* 编辑时出现清楚按钮 */

        textField.clearButtonMode = .unlessEditing/* 编辑时不出现,编辑之后才出现清楚按钮 */

        textField.clearButtonMode = .always/* 一直显示清楚按钮 */

        

        

        /* 7. 设置输入时得键盘类型

            (1) Default          系统默认的虚拟键盘

            (2) ASCII Capable    显示英文字母的虚拟键盘(可以输入英文和数字)

            (3) Numbers and Punctuation  显示数字和标点的虚拟键盘

            (4) URL               系那是便于输入url网址的虚拟键盘

            (5) Number Pad        显示便于输入数字的虚拟键盘(纯数字,没有小数点)

            (6) Phone Pad         显示便于拨号呼叫的虚拟键盘 (数字, + * #)

            (7) Name Phone Pad    显示便于聊天拨号的虚拟键盘 (会默认中文输入)

            (8) Email Address     显示便于输入Email的虚拟键盘

            (9) Decimal Pad       显示用于输入数字和小数点的虚拟键盘(数字+小数点)

            (10)Twitter           显示方便些Twitter的虚拟键盘

            (11)Web Search        显示便于在网页上书写的虚拟键盘

         

          常用:

            (1) Default      (可以输入中文,字母,数字)

            (2) ASCII Capable (显示英文字母和数字 ,键盘可以切换到字母,中文)

            (3) Number Pad    (0~9)

            (4) Decimal Pad   (0~9 .)

         */

        textField.keyboardType = .numbersAndPunctuation

        

        

        

        

        /* 8. 设置焦点(键盘弹起)  失去焦点(键盘消失) */

        textField.becomeFirstResponder() /* 成为第一响应者 */

        textField.resignFirstResponder() /* 失去第一响应者 */

        

        

        

        

        /* 9. 设置键盘return的样式

         

            .done    return显示"完成"字样

            .go      return显示"前往"字样

            .search  return显示"搜索"字样

            .join    return显示"加入"字样

            .next    return显示"下一项"字样

            .send    return显示"发送"字样

         */

        textField.returnKeyType = .send

        

        

        /* 10. 键盘return键的响应 

            /* 先设置textField的代理 */

            textField.delegate = self

            /* 然后实现shouldReturn方法 */

            func textFieldShouldReturn(_ textField: UITextField) -> Bool{}

         */

        

        

        

        /* 11. 密码模式 */

        textField.isSecureTextEntry =true

        

        /* 12. 修改olaceHolder字体的颜色 */

        textField.attributedPlaceholder =NSAttributedString.init(string:"输入用户名", attributes: [NSForegroundColorAttributeName:UIColor.red])

        

        

        

        /* 13. 

             粘贴文本示英文改成中文

             plist文件 Localization native development region en改为china

         

             禁止粘贴,选择

             重写UITextField

                - (BOOL)canPerformAction:(SEL)action withSender:(id)sender

                {

                  if (action == @selector(paste:))//禁止粘贴

                      return NO;

                  if (action == @selector(select:))//禁止选择

                      return NO;

                  if (action == @selector(selectAll:))//禁止全选

                      return NO;

                  return [super canPerformAction:action withSender:sender];

                 }

         */

        

        

         /* 14 关于输入字符太靠左边边缘的问题 (leftView)

         

             (1) 设置UITextField leftViewMode .always

             (2) 设置leftView

         */

        let leftView =UIView()

        leftView.frame =CGRect.init(x:0, y: 0, width:10, height: 10)

        textField.leftViewMode = .always

        textField.leftView = leftView

0 0