UIButton-按鈕
按鈕的創(chuàng)建
let button:UIButton = UIButton(type: UIButton.ButtonType.contactAdd)
button.frame = CGRect(x:100, y:100, width:100, height:100)
//設(shè)置按鈕的背景顏色
button.backgroundColor = UIColor.cyan
//設(shè)置按鈕的背景圖片
button.setBackgroundImage(UIImage(named:"background"),for:.normal)
//對于Custom定制類型按鈕,代碼可簡化為:
let button1 = UIButton(frame: CGRect(x:100, y:100, width:100, height:100))
//設(shè)置按鈕文字
button.setTitle("點我一下", for: UIControlState.normal)
//設(shè)置button的點擊事件
button.addTarget(self, action: #selector(ViewController.clickButton(_:)), for: UIControl.Event.touchUpInside)
self.view.addSubview(button)
按鈕有下面四種類型:
- UIButtonType.ContactAdd:前面帶“+”圖標按鈕,默認文字顏色為藍色,有觸摸時的高亮效果
- UIButtonType.DetailDisclosure:前面帶“!”圖標按鈕,默認文字顏色為藍色,有觸摸時的高亮效果
- UIButtonType.System:前面不帶圖標,默認文字顏色為藍色,有觸摸時的高亮效果
- UIButtonType.Custom:定制按鈕,前面不帶圖標,默認文字顏色為白色,無觸摸時的高亮效果
- UIButtonType.InfoDark:為感嘆號“!”圓形按鈕
- UIButtonType.InfoLight:為感嘆號“!”圓形按鈕
按鈕的文字設(shè)置
- UIControlState.Normal:普通狀態(tài)下的文字
- UIControlState.Highlighted:觸摸狀態(tài)下的文字
- UIControlState.Disabled:禁用狀態(tài)下的文字
按鈕文字顏色的設(shè)置
button.setTitleColor(UIColor.black,for: .normal) //普通狀態(tài)下文字的顏色
button.setTitleColor(UIColor.green,for: .highlighted) //觸摸狀態(tài)下文字的顏色
button.setTitleColor(UIColor.gray,for: .disabled) //禁用狀態(tài)下文字的顏色
按鈕文字陰影顏色的設(shè)置
button.setTitleShadowColor(UIColor.green,for:.normal) //普通狀態(tài)下文字陰影的顏色
button.setTitleShadowColor(UIColor.yellow,for:.highlighted) //普通狀態(tài)下文字陰影的顏色
button.setTitleShadowColor(UIColor.gray,for:.disabled) //普通狀態(tài)下文字陰影的顏色
按鈕文字圖標的設(shè)置
button.setImage(UIImage(named:"pic"),for:.normal) //設(shè)置圖標
button.adjustsImageWhenHighlighted=false //使觸摸模式下按鈕也不會變暗
button.adjustsImageWhenDisabled=false //使禁用模式下按鈕也不會變暗
常用的觸摸事件類型
- TouchDown:單點觸摸按下事件,點觸屏幕
- TouchDownRepeat:多點觸摸按下事件,點觸計數(shù)大于1,按下第2、3或第4根手指的時候
- TouchDragInside:觸摸在控件內(nèi)拖動時
- TouchDragOutside:觸摸在控件外拖動時
- TouchDragEnter:觸摸從控件之外拖動到內(nèi)部時
- TouchDragExit:觸摸從控件內(nèi)部拖動到外部時
- TouchUpInside:在控件之內(nèi)觸摸并抬起事件
- TouchUpOutside:在控件之外觸摸抬起事件
- TouchCancel:觸摸取消事件,即一次觸摸因為放上太多手指而被取消,或者電話打斷
設(shè)置按鈕的圓角
button.layer.masksToBounds = true
button.layer.cornerRadius = 6.0
設(shè)置按鈕點擊時高亮,默認點擊時是高亮的狀態(tài)
button.showsTouchWhenHighlighted = true
UIButton在開發(fā)的過程中應(yīng)用比較多,以后還會繼續(xù)補充的,請大家多多關(guān)注。
UITextField-文本輸入框
1.文本輸入框的創(chuàng)建
let textF = UITextField(frame: CGRect(x:50,y:300,width:100,height:30))
//設(shè)置文肯輸入框的邊框樣式
textF.borderStyle = UITextBorderStyle.none
//設(shè)置文本框的提示文字
textF.placeholder="請輸入"
//設(shè)置用*顯示,比如密碼 swift3.0廢棄
// textF.secureTextEntry = true
//設(shè)置文本輸入框的背景顏色
textF.backgroundColor = UIColor.white
self.view.addSubview(textF)
文本框的樣式:
- UITextField.BorderStyle.none:無邊框
- UITextField.BorderStyle.line:直線邊框
- UITextField.BorderStyle.roundedRect:圓角矩形邊框
- UITextBorderStyle.bezel:邊線+陰影
文字大小超過文本框長度時自動縮小字號
//當文字超出文本框?qū)挾葧r,自動調(diào)整文字大小
textF.adjustsFontSizeToFitWidth=true
textF.minimumFontSize=14 //最小可縮小的字號
設(shè)置水平/垂直對齊方式
//水平對齊
textF.textAlignment = .right //水平右對齊
textF.textAlignment = .center //水平居中對齊
textF.textAlignment = .left //水平左對齊
// 垂直對齊
textF.contentVerticalAlignment = .top //垂直向上對齊
textF.contentVerticalAlignment = .center //垂直居中對齊
textF.contentVerticalAlignment = .bottom //垂直向下對齊
設(shè)置背景圖片
textF.borderStyle = UITextField.BorderStyle.none //先要去除邊框樣式
textF.background = UIImage(named:"background")
設(shè)置輸入框右邊的小叉叉(清除按鈕)
textF.clearButtonMode = UITextField.ViewMode.whileEditing //編輯時出現(xiàn)清除按鈕
textF.clearButtonMode = UITextField.ViewMode.unlessEditing //編輯時不出現(xiàn),編輯后才出現(xiàn)清除按鈕
textF.clearButtonMode = UITextField.ViewMode.always //一直顯示清除按鈕
設(shè)置文本框關(guān)聯(lián)的鍵盤類型
textF.keyboardType = UIKeyboardType.default //系統(tǒng)默認的虛擬鍵盤
//顯示英文字母的虛擬鍵盤 swift3.0廢棄
// textF.keyboardType = UIKeyboardType.aSCIICapable
textF.keyboardType = UIKeyboardType.numbersAndPunctuation //顯示數(shù)字和標點的虛擬鍵盤
textF.keyboardType = UIKeyboardType.URL //顯示便于輸入數(shù)字的虛擬鍵盤
textF.keyboardType = UIKeyboardType.numberPad //顯示便于輸入數(shù)字的虛擬鍵盤
textF.keyboardType = UIKeyboardType.phonePad //顯示便于撥號呼叫的虛擬鍵盤
textF.keyboardType = UIKeyboardType.namePhonePad //顯示便于聊天撥號的虛擬鍵盤
textF.keyboardType = UIKeyboardType.emailAddress //顯示便于輸入Email的虛擬鍵盤
textF.keyboardType = UIKeyboardType.decimalPad //顯示用于輸入數(shù)字和小數(shù)點的虛擬鍵盤
textF.keyboardType = UIKeyboardType.twitter //顯示方便些Twitter的虛擬鍵盤
textF.keyboardType = UIKeyboardType.webSearch //顯示便于在網(wǎng)頁上書寫的虛擬鍵盤
使文本框在界面打開時就獲取焦點,并彈出輸入鍵盤
textF.becomeFirstResponder()
使文本框失去焦點,并收回鍵盤
textF.resignFirstResponder()
設(shè)置鍵盤return鍵的樣式
textF.returnKeyType = UIReturnKeyType.done//表示完成輸入
textF.returnKeyType = UIReturnKeyType.go //表示完成輸入,同時會跳到另一頁
textF.returnKeyType = UIReturnKeyType.search //表示搜索
textF.returnKeyType = UIReturnKeyType.join //表示注冊用戶或添加數(shù)據(jù)
textF.returnKeyType = UIReturnKeyType.next //表示繼續(xù)下一步
textF.returnKeyType = UIReturnKeyType.send //表示發(fā)送
鍵盤return鍵的響應(yīng)
class ViewController: UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let textField = UITextField(frame: CGRect(x:10,y:160,width:200,height:30))
//設(shè)置邊框樣式為圓角矩形
textField.borderStyle = UITextBorderStyle.roundedRect
textField.returnKeyType = UIReturnKeyType.done
textField.delegate=self
self.view.addSubview(textField)
}
func textFieldShouldReturn(_ textField:UITextField) -> Bool {
//收起鍵盤
textField.resignFirstResponder()
return true;
}
}
2.關(guān)于UITextField的attributedPlaceholder屬性設(shè)置
我們在使用textfield的時候為了實現(xiàn)更美觀的效果,常常會設(shè)置placeholder的相關(guān)屬性,那就是attributedPlaceholder。
//創(chuàng)建屬性字典,存放我們想要修改的屬性
var attributes:[String:AnyObject] = NSMutableDictionary() as! [String:AnyObject]
attributes[NSFontAttributeName] = UIFont.systemFont(ofSize: 13)
//創(chuàng)建帶屬性的字符串
let string:NSAttributedString = NSAttributedString.init(string: "身份證號中含字母必須大寫", attributes: attributes)
//設(shè)置placeholder的屬性
textF.attributedPlaceholder = string
3.關(guān)于UITextField的光標的設(shè)置
這個demo中主要是實現(xiàn)以leftView為左邊界,并且一直顯示,使光標的位置右移。
let textF = UITextField(frame: CGRect(x:50,y:300,width:100,height:30))
textF.backgroundColor = UIColor.yellow
self.view.addSubview(textF)
textF.leftView = UIView(frame: CGRect(x:0,y:0,width:30,height:30))
textF.leftView?.backgroundColor = UIColor.blue
textF.leftViewMode = UITextFieldViewMode.always
4.UITextField的代理方法
//將要開始輸入時調(diào)用
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("開始輸入")
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
print("已經(jīng)開始輸入")
}
//輸入結(jié)束時調(diào)用
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("輸入結(jié)束")
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("已經(jīng)結(jié)束輸入")
}
//清除文字按鈕點擊事件
func textFieldShouldClear(_ textField: UITextField) -> Bool {
print("清除輸入內(nèi)容")
return true
}
//鍵盤上的return按鈕
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//隱藏輸入鍵盤
textField.resignFirstResponder()
return true
}
5.storyboard/xib中設(shè)置placeholder的顏色
我們在用storyboard/xib中可能會發(fā)現(xiàn)并沒有設(shè)置textField的placeholder的顏色的屬性,其實我們可以這樣做:
設(shè)置placeholder.png
在keyPath中寫入placeholderLabel.textColor,Type選擇Color,Value就是設(shè)置自己喜歡的顏色啦。以后我們需要其他屬性的時候也可以按照這種方法添加哦。