// // IBTextField.swift // MTP2_iOS // // Created by zhongyuan on 2018/3/17. // Copyright © 2018年 zhongyuan.All rights reserved. // import UIKit @IBDesignable class IBTextField: UITextField { /// 集成IBView 参数 @IBInspectable var 圆角偏移量: CGFloat = 0.0 @IBInspectable var 圆角倍数: CGPoint = CGPoint.init(x: 0, y: 0) @IBInspectable var maskToBounds: Bool = false @IBInspectable var borderColor: UIColor = UIColor.clear @IBInspectable var borderWidth: CGFloat = 0.0 /// IBTextField参数 @IBInspectable var insetLeft: CGFloat = 0.0 @IBInspectable var insetRight: CGFloat = 0.0 @IBInspectable var isNeedRightView: Bool = false { didSet { if !isNeedRightView { rightView = nil rightViewMode = .always return } rightView = UIImageView(image: rightImage) (rightView as? UIImageView)?.contentMode = .scaleAspectFit rightViewMode = .always } } @IBInspectable var rightImage: UIImage? = UIImage(named: "pull") { didSet { if !isNeedRightView { return } rightView = UIImageView(image: rightImage) rightViewMode = .always } } @IBInspectable var rightImageRightInsert: CGFloat = 0.0 private static let checkValueBlock = {(rect: CGRect,target: IBTextField) -> CGRect in guard let l:Double = target.value(forKey:"insetLeft") as? Double, let r:Double = target.value(forKey:"insetRight") as? Double else { return CGRect.init() } return CGRect.init(x: CGFloat(l), y: 0, width: rect.size.width - CGFloat(l) - CGFloat(r), height: rect.size.height) } override func layoutSubviews() { super.layoutSubviews() IBView.refreshIBEffect(view: self) } /// UITextField 文字与输入框的距离 override func textRect(forBounds bounds:CGRect) -> CGRect { let rect:CGRect = super.textRect(forBounds: bounds) return IBTextField.checkValueBlock(rect, self) } /// 控制文本的位置 override func editingRect(forBounds bounds: CGRect) -> CGRect { let rect:CGRect = super.editingRect(forBounds: bounds) return IBTextField.checkValueBlock(rect, self) } /// 控制rightView的距离 override func rightViewRect(forBounds bounds: CGRect) -> CGRect { var rect:CGRect = super.rightViewRect(forBounds: bounds) rect.origin.x -= rightImageRightInsert return rect } }