//
// ViewController.swift
// UITextField鍵盤類型
//
// Created by mac on 15/4/19.
// Copyright (c) 2015年 mac. All rights reserved.
//
import UIKit
class ViewController: UIViewController ,UITextFieldDelegate{
var text = UITextField()
var pwdtext = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
// Default // Default type for the current input method.
// case ASCIICapable // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
// case NumbersAndPunctuation // Numbers and assorted punctuation.
// case URL // A type optimized for URL entry (shows . / .com prominently).
// case NumberPad // A number pad (0-9). Suitable for PIN entry.
// case PhonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
// case NamePhonePad // A type optimized for entering a person's name or phone number.
// case EmailAddress // A type optimized for multiple email address entry (shows space @ . prominently).
// case DecimalPad // A number pad with a decimal point.
// case Twitter // A type optimized for twitter text entry (easy access to @ #)
// case WebSearch // A default keyboard type with URL-oriented addition (shows space . prominently).
// 設置背景圖片
var imageview = UIImageView(frame: self.view.bounds)
let image = UIImage(named: "1.png")
imageview.image = image
self.view.addSubview(imageview)
// 定義用戶名輸入框
var text = UITextField(frame: CGRectMake(30, 100, UIScreen.mainScreen().bounds.size.width-60, 30))
text.delegate = self
text.keyboardType = UIKeyboardType.Default
text.layer.borderWidth = 0.5
text.layer.borderColor = UIColor.lightGrayColor().CGColor
text.placeholder = "請輸入用戶名"
text.clearButtonMode = UITextFieldViewMode.Always
self.view.addSubview(text)
text.returnKeyType = UIReturnKeyType.Done
self.text = text
let lab = UILabel(frame: CGRectMake(0, 0, 70, text.bounds.size.height))
lab.text = "用戶名:"
text.leftView = lab
// lab.font = UIFont.systemFontOfSize(17.0)
text.leftViewMode = UITextFieldViewMode.Always
var pwdtext = UITextField(frame: CGRectMake(30, 150, UIScreen.mainScreen().bounds.size.width-60, 30))
pwdtext.delegate = self
pwdtext.keyboardType = UIKeyboardType.NumberPad
pwdtext.layer.borderWidth = 0.5
pwdtext.layer.borderColor = UIColor.lightGrayColor().CGColor
pwdtext.placeholder = "請輸入文字"
pwdtext.clearButtonMode = UITextFieldViewMode.WhileEditing
self.view.addSubview(pwdtext)
pwdtext.returnKeyType = UIReturnKeyType.Done
self.pwdtext = pwdtext
let pwd = UILabel(frame: CGRectMake(0, 0, 70, text.bounds.size.height))
pwd.text = "驗證碼:"
pwdtext.leftView = pwd
pwd.font = UIFont.systemFontOfSize(17.0)
pwdtext.leftViewMode = UITextFieldViewMode.Always
// Do any additional seuitup after loading the view, typically from a nib.
}
// 第一種是遵循代理,是在代理方法中實現鍵盤的隱藏
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 第一種方法。用代理實現鍵盤的隱藏
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField == self.text) {
textField.resignFirstResponder()
// self.view.becomeFirstResponder()
}
return true;
}
// 第二種方法實現鍵盤的隱藏
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.text.resignFirstResponder()
self.pwdtext.resignFirstResponder()
}
}