实现效果:
点击进入输入状态时placeholder变小上移,退出输入状态时若是textfield为空则恢复原状.
密码类型有明密文转换按钮.
使用extension实现通过修改属性控制最大输入数,默认15.
#实现思路:
使用自定制的label代替placeholder,在代理中判断textfield当前状态.根据输入状态来修改自定制label的状态.
给textfield加extension,通过runtime为其加最大输入数属性,使其实现方法来控制最大输入数.
实现方式:
1.重写textfield,来实现我们需要的样式.
2.为textfield加拓展,来实现最大输入数控制.
3.实现动画效果.
4.在代理方法中判断状态调用textfield的动画方法
1.重写textfield,来实现我们需要的样式.
private let bottomLine : CALayer = {
let line = CALayer()
line.backgroundColor = UIColor.x4FB0FF.cgColor
line.isHidden = true
return line
}()
public let label : UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 18)
label.textColor = UIColor.x333333_alpha40
label.textAlignment = .left
label.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
return label
}()
/// init方法
///
/// - Parameters:
/// - frame: frame
/// - isSecure: 是否是密码格式
init(frame:CGRect,isSecure:Bool){
super.init(frame:frame)
self.isSecureTextEntry = isSecure
drawMyView()
/// 添加字数判断
addChangeTextTarget()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func drawMyView() {
self.addSubview(label)
self.layer.addSublayer(bottomLine)
if self.isSecureTextEntry {
let passwordSwitch = PassowrdSwitch(frame: CGRect(x: 0, y: 0, width: 21, height: 12.5))
passwordSwitch.addTarget(self, action: #selector(togglePasswordHidden(sender:)), for: .touchUpInside)
self.rightView = passwordSwitch
self.rightViewMode = .always
}
}
2.为textfield加拓展,来实现最大输入数控制.
var maxTextNumberDefault = 15
extension UITextField{
/// 使用runtime给textField添加最大输入数属性,默认15
var maxTextNumber: Int {
set {
objc_setAssociatedObject(self, &maxTextNumberDefault, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
if let rs = objc_getAssociatedObject(self, &maxTextNumberDefault) as? Int {
return rs
}
return 15
}
}
/// 添加判断数量方法
func addChangeTextTarget(){
self.addTarget(self, action: #selector(changeText), for: .editingChanged)
}
@objc func changeText(){
//判断是不是在拼音状态,拼音状态不截取文本
if let positionRange = self.markedTextRange{
guard self.position(from: positionRange.start, offset: 0) != nil else {
checkTextFieldText()
return
}
}else {
checkTextFieldText()
}
}
/// 判断已输入字数是否超过设置的最大数.如果是则截取
func checkTextFieldText(){
guard (self.text?.length)! <= maxTextNumber else {
self.text = (self.text?.stringCut(end: maxTextNumber))!
return
}
}
}
3.实现动画效果.
/// 将placeholder上移方法,点击空的textfield时调用
public func changeLabel(){
UIView.animate(withDuration: 0.4) {
self.label.frame = CGRect(x: 0, y: -20, width: 100, height: 20)
self.label.font = UIFont.systemFont(ofSize: 10)
self.label.textColor = UIColor.x4FB0FF
}
}
public func changeLineHidden(){
self.bottomLine.isHidden = !self.bottomLine.isHidden
}
/// placeholder下移方法,当文字清空时调用
public func disChangeLabel(){
UIView.animate(withDuration: 0.4) {
self.label.frame = CGRect(x: 0, y: 0, width: 100, height: self.frame.size.height)
self.label.font = UIFont.systemFont(ofSize: 18)
self.label.textColor = UIColor.x333333_alpha40
}
}
4.在代理方法中判断状态调用textfield的动画方法
extension ViewController: UITextFieldDelegate{
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField is CRTextField {
let tf = textField as! CRTextField
if tf.text == ""{
tf.changeLabel()
}
tf.changeLineHidden()
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField is CRTextField {
let tf = textField as! CRTextField
if tf.text == "" {
tf.disChangeLabel()
}
tf.changeLineHidden()
}
}
}
github地址:EWTextField.
有问题欢迎探讨.
|
请发表评论