重写TextField Rect 改变显示位置

很简单很常用的一些东西,希望给需要的人帮助。

效果图如下:

重写TextField Rect 改变显示位置

自定义textField

init() {

        super.init(frame: CGRect(x: , y: , width: yourWidth, height: yourHeight))
      
     //光标颜色修改  
     self.tintColor = UIColor.orangeColor()
    }

override func editingRectForBounds(bounds: CGRect) -> CGRect {        

        return CGRectInset(bounds, , );

    }

    override func textRectForBounds(bounds: CGRect) -> CGRect {

        return CGRectInset(bounds, , )

    }

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {

                return CGRectInset(bounds, , )

    }

    override func leftViewRectForBounds(bounds: CGRect) -> CGRect {

        var iconRect = super.leftViewRectForBounds(bounds)

        iconRect.origin.x = iconRect.origin.x + 

        return iconRect

    }

然后在需要使用的controller调用即可

//其他非相关代码已省略

func setupMobelLoginUI(){
...
mobTxtFld.backgroundColor = LoginConst.tfBackgroundColor
mobTxtFld.layer.cornerRadius = LoginConst.commonHeight /
mobTxtFld.clipsToBounds = true
mobTxtFld.layer.borderColor = LoginConst.tfBorderColor.CGColor
mobTxtFld.layer.borderWidth = //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()
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 /
verifyCodeTxtFld.layer.masksToBounds = true
verifyCodeTxtFld.layer.borderColor = LoginConst.tfBorderColor.CGColor
verifyCodeTxtFld.layer.borderWidth = view.addSubview(verifyCodeTxtFld)
verifyCodeTxtFld.snp_makeConstraints { (make) in
make.top.equalTo(mobTxtFld.snp_bottom).offset(LoginConst.verifyVMargin)
make.width.height.centerX.equalTo(mobTxtFld)
}
...
}
上一篇:黑马程序员_Java基础:集合总结


下一篇:Backup and Recovery Basics1