多姿多彩的UITextField

2,039 阅读2分钟

唠叨

写了很长一段时间的业务代码了, 东瞧瞧, 西看看, 感觉自己都是在原地踏步. 为了自己的职业生涯更长一点, 也为了能让自己进步一点, 所以就有了这篇UITextField的使用总结, 当然这只是一个开始, 以后自己也会总结更多的东西, 更深入的研究个中原理和本质, 知其然和所以然!!! 努力吧!!!

UI展示

传送门

之前忘放地址了, 嘻嘻!!! GitHub

className

LCLTextField version - 0.1

新增功能

  1. placeholderColor: 占位符颜色
  2. maxLength: 文本最大输入长度
  3. isHiddenKeyboardIfEditing: 点击textField时, 如果键盘隐藏, 则显示键盘; 如果键盘显示, 则隐藏键盘
  4. textInsets: 添加内边距
  5. addBorderAll: 全部边框
  6. addBorderLeft: 添加左边框
  7. addBorderRight: 添加右边框
  8. addBorderTop: 添加上边框
  9. addBorderBottom: 添加下边框
  10. addCornerAll: 全部圆角
  11. addCorner: 添加指定的圆角

代码展示

  • 展示1

        let width = UIScreen.main.bounds.width
        
        let textField = LCLTextField()
        textField.frame = CGRect(x: 40, y: 100, width: width - 80, height: 40);
        textField.textAlignment = .left
        self.view.addSubview(textField)
        
        // 光标颜色
        textField.tintColor = .green
        
        // 文本颜色
        textField.textColor = .green
        
        // 提示语颜色
        textField.placeholderColor = .blue
        textField.placeholder = "请输入"
        
        // 最大文字长度
        textField.maxLength = 10
        
        // 点击textField, 如果处理编辑状态, 隐藏键盘
        textField.isHiddenKeyboardIfEditing = true
        
        // 添加内边距 - 上左下右
        textField.textInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        
        // 全部边框
        textField.addBorderAll(color: .red, borderWidth: 2)
        
        // 全部圆角
        textField.addCornerAll(radius: 10)
  • 展示2

        let width = UIScreen.main.bounds.width
        
        let textField = LCLTextField()
        textField.frame = CGRect(x: 40, y: 160, width: width - 80, height: 40);
        textField.textAlignment = .center
        self.view.addSubview(textField)
        
        // 光标颜色
        textField.tintColor = .orange
        
        // 文本颜色
        textField.textColor = .orange
        
        // 提示语颜色
        textField.placeholderColor = .green
        textField.placeholder = "请输入"

        // 最大文字长度
        textField.maxLength = 10
        
        // 点击textField, 如果处理编辑状态, 隐藏键盘
        textField.isHiddenKeyboardIfEditing = true
        
        // 添加内边距 - 上左下右
        textField.textInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        
        // 添加4条不同颜色的边框
        textField.addBorderLeft(color: .red, borderWidth: 2)
        textField.addBorderRight(color: .orange, borderWidth: 2)
        textField.addBorderTop(color: .yellow, borderWidth: 2)
        textField.addBorderBottom(color: .green, borderWidth: 2)
  • 展示3

        let width = UIScreen.main.bounds.width
        
        let textField = LCLTextField()
        textField.frame = CGRect(x: 40, y: 220, width: width - 80, height: 40);
        textField.backgroundColor = .cyan
        textField.textAlignment = .right
        self.view.addSubview(textField)
        
        // 光标颜色
        textField.tintColor = .white
        
        // 文本颜色
        textField.textColor = .white
        
        // 提示语颜色
        textField.placeholderColor = .lightGray
        textField.placeholder = "请输入"

        // 最大文字长度
        textField.maxLength = 10
        
        // 点击textField, 如果处理编辑状态, 隐藏键盘
        textField.isHiddenKeyboardIfEditing = true
        
        // 添加内边距 - 上左下右
        textField.textInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        
        // 圆角
        textField.AddCorner(isTopLeft: true, isTopRight: false, isBottomLeft: false, isBottomRight: true, radius: 20)

后记

其实, UITextField需要满足非常多的业务需求, 想要通过测试也需要非常多的考虑和限制, 当然我也被我们家的产品小姐姐和测试小姐姐盯着一条条的完善, 都是泪!!! 所以, 还有很多东西要写, 我会慢慢加上的!

再唠叨一句, 其实我建了个博客: 云淡风轻的博客