• [Xcode 实际操作]四、常用控件-(8)UITextField控件的使用


    目录:[Swift]Xcode实际操作

    本文将演示文本输入框控件的基本用法。

    文本输入框主要用来接收和显示用户输入的内容。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 
     3 //添加文本框代理协议,
     4 //使用协议中的方法,在完成文本框文字的输入后,隐藏系统键盘的显示
     5 class ViewController: UIViewController, UITextFieldDelegate {
     6 
     7     override func viewDidLoad() {
     8         super.viewDidLoad()
     9         // Do any additional setup after loading the view, typically from a nib.
    10         //创建一个位置在(60,80),尺寸为(200,30)的显示区域
    11         let rect = CGRect(x: 60, y: 80,  200, height: 30)
    12         //初始化文本输入框对象,并设置其位置和尺寸
    13         let textField = UITextField(frame: rect)
    14         
    15         //设置文本框对象的边框样式为圆角矩形
    16         textField.borderStyle = UITextField.BorderStyle.roundedRect
    17         //设置文本框的占位符属性,用来描述输入字段预期值的提示信息,
    18         //该提示会在输入字段为空时显示,并会在字段获得焦点时小时
    19         textField.placeholder = "Your Email"
    20         //关闭文本框对象的语法错误提示功能
    21         textField.autocorrectionType = UITextAutocorrectionType.no
    22         //设置在输入文字时,在键盘面板上,回车按钮的类型。
    23         textField.returnKeyType = UIReturnKeyType.done
    24         //设置文本框对象右侧的清除按钮,仅在编辑状态时显示
    25         textField.clearButtonMode = UITextField.ViewMode.whileEditing
    26         //设置文本框对象的键盘类型,为系统提供的邮箱地址类型
    27         textField.keyboardType = UIKeyboardType.emailAddress
    28         //设置文本框对象的键盘为暗色主题
    29         textField.keyboardAppearance = UIKeyboardAppearance.dark
    30         
    31         //设置文本框对象的代理为当前视图控制器
    32         textField.delegate = self
    33         
    34         //将文本框对象,添加到当前视图控制器的根视图
    35         self.view.addSubview(textField)
    36     }
    37     
    38     //添加一个方法,当用户按下键盘上的回车键时,调用此方法。
    39     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    40         //当用户按下键盘上的回车键时,
    41         //文本框失去焦点,键盘也将自动隐藏
    42         textField.resignFirstResponder()
    43         //在方法的末尾返回一个布尔值
    44         return true
    45     }
    46 
    47     override func didReceiveMemoryWarning() {
    48         super.didReceiveMemoryWarning()
    49         // Dispose of any resources that can be recreated.
    50     }
    51 }
  • 相关阅读:
    Spring jar包功能
    maven pom.xml详细介绍,必须留一份
    redis基本类型以及优点特性
    如何实现网站的防盗链?
    php几种常见排序算法
    批量去除文件的BOM头
    Keepalived实现双机热备
    nginx配置文件详解
    源码编译安装lnmp环境
    nginx实现https网站设置
  • 原文地址:https://www.cnblogs.com/strengthen/p/10016065.html
Copyright © 2020-2023  润新知