zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
    简单几何(推公式) UVA 11646 Athletics Track
    简单几何(四边形形状) UVA 11800 Determine the Shape
    简单几何(求交点) UVA 11437 Triangle Fun
    计算几何模板
    简单几何(相对运动距离最值) UVA 11796 Dog Distance
    简单几何(求划分区域) LA 3263 That Nice Euler Circuit
    覆盖的面积 HDU
    Desert King 最小比率生成树 (好题)
    约会安排 (区间合并)毒瘤题
  • 原文地址:https://www.cnblogs.com/strengthen/p/10016065.html
Copyright © 2011-2022 走看看