自定义TextField(宽高可改变),内部文字左右缩进的实现

TextField的宽高是可以改变的,只要添加到视图的时候添加约束设置他的宽高即可,输入开始位置和结束位置也是可以设置的,示例代码如下:

    //MARK:- 创建不带按钮的输入框
    func createPlainTextField(backgroundImageName: String, placeHolderName: String, textColor: UIColor) -> UITextField {
        let textField = UITextField()
        textField.background = UIImage(named: backgroundImageName)
        textField.font = UIFont.systemFontOfSize(32.layoutVal)
        textField.textColor = textColor

        //设置placeHolder的字体大小和颜色
        textField.placeholder = placeHolderName
        textField.setValue(SXColors.ccNavBarAndLableShadow, forKeyPath: "_placeholderLabel.textColor")
        textField.setValue(UIFont.systemFontOfSize(32.layoutVal), forKeyPath: "_placeholderLabel.font")

        //设置开始输入文字的位置
        let leftView = UILabel()
        textField.leftView = leftView
        leftView.bounds = CGRectMake(0, 0, 48.layoutVal, textField.height)
        leftView.backgroundColor = UIColor.clearColor()
        textField.leftViewMode = .Always
        textField.contentVerticalAlignment = .Center

        return textField
    }

你可能感兴趣的:(iOS开发,swift开发)