一、视图控件
class ZJFieldView: UIView ,UITextFieldDelegate{
override init(frame: CGRect) {
super.init(frame: frame);
self.addSubview(self.leftImgView);
self.addSubview(self.centerFeild);
self.addSubview(self.rightImgView);
self.backgroundColor = UIColor.orange;
self.leftImgView.backgroundColor = UIColor.red;
self.rightImgView.backgroundColor = UIColor.red;
self.centerFeild.delegate = self;
self.centerFeild.keyboardType = UIKeyboardType.asciiCapable;
self.centerFeild.addTarget(self, action: #selector(fiedViewTextChanged(_ :)), for: UIControlEvents.editingChanged);
self.rightImgView.addObserver(self, forKeyPath: "selected", options: NSKeyValueObservingOptions.new, context: nil);
self.rightImgView.addTarget(self, action: #selector(rightImgViewClick(_ :)), for: UIControlEvents.touchUpInside);
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*********************************************/
private var leftImgView = UIImageView();
private var centerFeild = UITextField();
private var rightImgView = UIButton();
private var fieldBlock:((String)->())?
/*********************************************/
@objc private func fiedViewTextChanged(_ field:UITextField){
if(self.fieldBlock != nil){
self.fieldBlock!(field.text!);
}
}
@objc private func rightImgViewClick(_ tempBtn:UIButton){
tempBtn.isSelected = !tempBtn.isSelected;
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "selected"){
self.centerFeild.isEnabled = false;
self.centerFeild.isSecureTextEntry = self.rightImgView.isSelected;
self.centerFeild.isEnabled = true;
self.centerFeild.becomeFirstResponder();
}
}
/*********************************************/
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if(textField.isSecureTextEntry == true){
let tempString:NSString = textField.text! as NSString;
textField.text = tempString.replacingCharacters(in: range, with: string);
if(self.fieldBlock != nil){
self.fieldBlock!(textField.text!);
}
return false;
}
return true;
}
/*********************************************/
func setLeftFrame(frame subframe:CGRect){
self.leftImgView.frame = subframe;
}
func setCenterFrame(frame subframe:CGRect){
self.centerFeild.frame = subframe;
}
func setlRightFrame(frame subframe:CGRect){
self.rightImgView.frame = subframe;
}
func fieldChangedBlock(block tempBlock:@escaping(String)->()){
self.fieldBlock = tempBlock;
}
func setRightImgViewImage(image tempImage:UIImage? ,state tempState:UIControlState){
self.rightImgView.setBackgroundImage(tempImage, for: tempState);
}
}
二、使用:
var subframe = CGRect(x: 10, y: 10+64, width: KWidth-20, height: 40);
let fieldView = ZJFieldView(frame: subframe);
self.view.addSubview(fieldView);
subframe = CGRect(x: 0, y: 0, width: 40, height: 40);
fieldView.setLeftFrame(frame: subframe);
subframe = CGRect(x: 40, y: 0, width: fieldView.Width-80, height: 40);
fieldView.setCenterFrame(frame: subframe);
subframe = CGRect(x: fieldView.Width-40, y: 0, width: 40, height: 40);
fieldView.setlRightFrame(frame: subframe);
fieldView.setRightImgViewImage(image: UIImage(named: ""), state: UIControlState.normal);
fieldView.setRightImgViewImage(image: UIImage(named: ""), state: UIControlState.selected);
fieldView.fieldChangedBlock { (text) in
ZJLog(text);
}