ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 |
---|---|---|---|---|---|---|---|
0 | NUT | 32 | (space) | 64 | @ | 96 | 、 |
1 | SOH | 33 | ! | 65 | A | 97 | a |
2 | STX | 34 | " | 66 | B | 98 | b |
3 | ETX | 35 | # | 67 | C | 99 | c |
4 | EOT | 36 | $ | 68 | D | 100 | d |
5 | ENQ | 37 | % | 69 | E | 101 | e |
6 | ACK | 38 | & | 70 | F | 102 | f |
7 | BEL | 39 | , | 71 | G | 103 | g |
8 | BS | 40 | ( | 72 | H | 104 | h |
9 | HT | 41 | ) | 73 | I | 105 | i |
10 | LF | 42 | * | 74 | J | 106 | j |
11 | VT | 43 | + | 75 | K | 107 | k |
12 | FF | 44 | , | 76 | L | 108 | l |
13 | CR | 45 | - | 77 | M | 109 | m |
14 | SO | 46 | . | 78 | N | 110 | n |
15 | SI | 47 | / | 79 | O | 111 | o |
16 | DLE | 48 | 0 | 80 | P | 112 | p |
17 | DCI | 49 | 1 | 81 | Q | 113 | q |
18 | DC2 | 50 | 2 | 82 | R | 114 | r |
19 | DC3 | 51 | 3 | 83 | S | 115 | s |
20 | DC4 | 52 | 4 | 84 | T | 116 | t |
21 | NAK | 53 | 5 | 85 | U | 117 | u |
22 | SYN | 54 | 6 | 86 | V | 118 | v |
23 | TB | 55 | 7 | 87 | W | 119 | w |
24 | CAN | 56 | 8 | 88 | X | 120 | x |
25 | EM | 57 | 9 | 89 | Y | 121 | y |
26 | SUB | 58 | : | 90 | Z | 122 | z |
27 | ESC | 59 | ; | 91 | [ | 123 | { |
28 | FS | 60 | < | 92 | / | 124 | | |
29 | GS | 61 | = | 93 | ] | 125 | } |
30 | RS | 62 | > | 94 | ^ | 126 | ` |
31 | US | 63 | ? | 95 | _ | 127 | DEL |
自定義的UITextView來做統一限制
//
// PwdUiTextField.swift
// wulian
//
// Created by 陝西幫你電子科技有限公司 on 2019/3/1.
// Copyright © 2019 陝西幫你電子科技有限公司. All rights reserved.
//常用的正則表達式
//非中文:[^\\u4E00-\\u9FA5]
//非英文:[^A-Za-z]
//非數字:[^0-9]
//非中文或英文:[^A-Za-z\\u4E00-\\u9FA5]
//非英文或數字:[^A-Za-z0-9]
//非因爲或數字或下劃線:[^A-Za-z0-9_]
//非中文英文或數字:[^\\u4E00-\\u9FA5\\A-Za-z0-9]$
//
import UIKit
/**
輸入框的類型
*/
enum InputType:String{
case ZhongWen = "中文輸入"
case YingWen = "英文輸入"
case YingWenHeShuZi = "英文和數字輸入"
case NotCheck = "不進行驗證"
}
class MyUiTextField: UITextField,UITextFieldDelegate{
//最大輸入的長度
private var maxLength = 50
private var textFieldType = 0 //輸入框的限制輸入類型 0:中文英文或數字 1:英文 2:英文和數字 3:中文
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
}
private func initView(){
self.delegate = self
self.addTarget(self, action: #selector(textDidChange), for: .editingChanged)
self.backgroundColor = UIColor.withHex(hexString: "#FFFFFF", alpha: 0.2)
self.textColor = UIColor.white
//self.borderStyle = UITextBorderStyle. //設置圓角邊框
self.viewSettingRadius(5) //角度爲 5
self.clearButtonMode = UITextFieldViewMode.whileEditing //清除按鈕的顯示狀態 - 開始編輯時出現
// 完成按鈕樣式
self.returnKeyType = UIReturnKeyType.done
self.tintColor = UIColor.white //設置光標顏色
self.textAlignment = .left //設置文字對齊方式 - 左對齊
self.settingClearButtonModeStyle()
self.setModifyClearButton()//自定義清除按鈕
self.font = UIFont.systemFont(ofSize: 15)
self.returnKeyType = UIReturnKeyType.done //表示完成輸入
self.adjustsFontSizeToFitWidth=true //當文字超出文本框寬度時,自動調整文字大小
self.minimumFontSize=13 //最小可縮小的字號
self.ld_height = 40 //設置高度
}
@objc func textDidChange(_ textField:UITextField){
print("輸入的字符1:\(textField.text)")
//非markedText才繼續往下處理
guard let _: UITextRange = self.markedTextRange else{
//當前光標的位置(後面會對其做修改)
let cursorPostion = self.offset(from: textField.endOfDocument,
to: textField.selectedTextRange!.end)
//判斷非中文的正則表達式
var pattern = ""
if textFieldType == 0{
pattern = "[^\\u4E00-\\u9FA5\\A-Za-z0-9]$"
}else if textFieldType == 1{
pattern = "[^A-Za-z]"
}else if textFieldType == 2{
pattern = "[^A-Za-z0-9]"
}else if textFieldType == 3{
pattern = "[^\\u4E00-\\u9FA5]"
}
if textFieldType != -1{
//替換後的字符串(過濾調非中文字符)
let str = self.text!.pregReplace(pattern: pattern, with: "")
textField.text = str
//讓光標停留在正確位置
let targetPostion = self.position(from: textField.endOfDocument,
offset: cursorPostion)!
textField.selectedTextRange = self.textRange(from: targetPostion,
to: targetPostion)
}
return
}
}
//此處用來限制字符輸入的數量以及處理明文密文切換時之前的輸入不被清空
//true 爲可以輸入 false 爲不可以輸入
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var newText = NSString(string:textField.text!).replacingCharacters(in: range, with: string)
//獲取輸入的文本,移除向輸入框中粘貼文本時,系統自動加上的空格(iOS11上有該問題)
newText = newText.replacingOccurrences(of: " ", with: "")
print("maxlength:\(maxLength)")
print("newText:\(newText)")
if(newText.count) > maxLength{
//如果輸入的長度大於最大的限制,就將獲取從開始輸入的位置到最大限制的位置之間的索引
// let idx = self.text?.index((text?.startIndex)!, offsetBy: maxLength)
// //print("索引:\(idx)")
// //截取指定位置的字符
// self.text = String(self.text![self.text!.startIndex..<idx!])
self.text = String(newText.prefix(maxLength)) as String //截取指定最大字符的前幾個
return false
}
//只能輸入英文
if textFieldType == 1{
let length = string.lengthOfBytes(using: String.Encoding.utf8)
for loopIndex in 0..<length{
let char = (string as NSString).character(at: loopIndex)
//只能輸入 a~z A~Z
if char < 65 {return false}
if char >= 91 && char <= 112 {return false}
if char >= 123 {return false}
}
}else if textFieldType == 2{
//只能輸入英文和數字
let length = string.lengthOfBytes(using: String.Encoding.utf8)
for loopIndex in 0..<length{
let char = (string as NSString).character(at: loopIndex)
if char < 48 {return false }
if char > 57 && char < 65 {return false }
if char > 90 && char < 97 {return false}
if char > 122 { return false}
}
}
//如果當前的輸入框爲密碼輸入框
if textField == self && self.isSecureTextEntry{
self.text = newText
return false
}
return true
}
// override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// if action == #selector(paste(_:)){
// return false //禁止粘貼
// }else if action == #selector(select(_:)){
// return false //禁止選擇
// }else{
// return super.canPerformAction(action, withSender: sender)
// }
// }
/**
設置輸入的限制類型
*/
func setInputType(type:InputType){
if type == InputType.ZhongWen{
textFieldType = 3
}else if type == InputType.YingWenHeShuZi{
textFieldType = 2
}else if type == InputType.YingWen{
textFieldType = 1
}else if type == InputType.NotCheck{
textFieldType = -1
}
}
/**
設置最大可輸入的長度
*/
func setMaxLength(length:Int){
self.maxLength = length
}
/**
將輸入框設置爲密碼類型
*/
func settingPwdType(){
self.isSecureTextEntry = true //設置爲密碼輸入框
self.settingRigthPwdImage(image_name: "login_pwd_bukejian")
}
//設置輸入框右邊的圖標
private func settingRigthPwdImage(image_name:String){
//創建右邊顯示的切換密明文的圖片
let leftView = UIImageView(image: UIImage(named: image_name))
leftView.contentMode = .center
//添加背景視圖
let bgView = UIView()
bgView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
bgView.addSubview(leftView)
leftView.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.width.height.equalTo(15)
}
self.rightView = bgView
self.rightViewMode = .always //一直顯示
//添加明密文切換的事件
let switchGes = UITapGestureRecognizer(target: self, action: #selector(switchInputTypeClick))
bgView.addGestureRecognizer(switchGes)
bgView.isUserInteractionEnabled = true
}
@objc func switchInputTypeClick(){
if self.isSecureTextEntry{
//當前爲密文
self.settingRigthPwdImage(image_name: "login_pwd_kejian")
//設置爲明文
self.isSecureTextEntry = false
}else{
//當前爲明文
self.settingRigthPwdImage(image_name: "login_pwd_bukejian")
//設置爲密文
self.isSecureTextEntry = true
}
}
}