很简单很常用的一些东西,希望给需要的人帮助。
效果图如下:
自定义textField
init() { super.init(frame: CGRect(x: 0, y: 0, width: yourWidth, height: yourHeight))
//光标颜色修改
self.tintColor = UIColor.orangeColor()
} override func editingRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, 45, 0); } override func textRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, 45, 0) } override func placeholderRectForBounds(bounds: CGRect) -> CGRect { return CGRectInset(bounds, 45, 0) } override func leftViewRectForBounds(bounds: CGRect) -> CGRect { var iconRect = super.leftViewRectForBounds(bounds) iconRect.origin.x = iconRect.origin.x + 20 return iconRect }
然后在需要使用的controller调用即可
//其他非相关代码已省略 func setupMobelLoginUI(){ ... mobTxtFld.backgroundColor = LoginConst.tfBackgroundColor mobTxtFld.layer.cornerRadius = LoginConst.commonHeight / 2 mobTxtFld.clipsToBounds = true mobTxtFld.layer.borderColor = LoginConst.tfBorderColor.CGColor mobTxtFld.layer.borderWidth = 1 //leftView相关设置 let mobIcon = UIImageView(image: UIImage(named: "login_mobile")) mobTxtFld.leftViewMode = UITextFieldViewMode.Always mobTxtFld.leftView = mobIcon view.addSubview(mobTxtFld) mobTxtFld.snp_makeConstraints { (make) in make.top.equalTo(titleLb.snp_bottom).offset(LoginConst.topVOffset) make.centerX.equalTo(self.view) make.width.equalTo(325) make.height.equalTo(LoginConst.commonHeight) }
//leftView相关设置 let codeIcon = UIImageView(image: UIImage(named: "login_yanzheng")) verifyCodeTxtFld.leftViewMode = UITextFieldViewMode.Always verifyCodeTxtFld.leftView = codeIcon verifyCodeTxtFld.backgroundColor = LoginConst.tfBackgroundColor verifyCodeTxtFld.layer.cornerRadius = LoginConst.commonHeight / 2 verifyCodeTxtFld.layer.masksToBounds = true verifyCodeTxtFld.layer.borderColor = LoginConst.tfBorderColor.CGColor verifyCodeTxtFld.layer.borderWidth = 1 view.addSubview(verifyCodeTxtFld) verifyCodeTxtFld.snp_makeConstraints { (make) in make.top.equalTo(mobTxtFld.snp_bottom).offset(LoginConst.verifyVMargin) make.width.height.centerX.equalTo(mobTxtFld) } ... }