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)
}
//限制输入字符的长度
if let toBeString = self.text {
///键盘输入模式(OC里面通过[[UITextInputMode currentInputMode] primaryLanguage]来获取当前输入法,Swift里面没有currentInputMode这个东西,只能是获取你正在使用的所有输入法,然后第一个就是当前输入法)
if let language = UITextInputMode.activeInputModes.first?.primaryLanguage {
if language == "zh-Hans" {///中文输入法
///这里跟OC也有点区别,直接如下这么写就行,只有在输入拼音选中后才会走到else里面,然后在else里面写条件判断就好了
if let _ = self.markedTextRange {
}else {
if toBeString.length > maxLength {
///swift里面处理字符串特别麻烦,这里非要用String.index 这种类型,然后我给string常用的一些方法都封装了一下。这里直接这么调用就可以
self.text = String(toBeString.prefix(maxLength)) as String
}
}
}else {///非中文输入法,直接统计字数和限制,这里没有考虑其他语种的情况
if toBeString.length > maxLength {
self.text = String(toBeString.prefix(maxLength)) as String
}
}
}
}
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..= 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.. 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
}
}
}