swift中UITextField的使用

在Swift编程中,UITextField的使用需要注意一个重要的细节:若同时设置了clearButtonMode和rightViewMode/rightView,系统只会应用rightViewMode/rightView的设置,clearButtonMode将失效。这对于布局和功能设计是个关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

let textfield = UITextField(frame: CGRectMake(10.0, 10.0, 200.0,40.0))
self.view.addSubview(textfield)
// 字体属性设置
textfield.textColor = UIColor.blackColor()
textfield.font = UIFont(name: "GillSans", size: 15.0)
textfield.textAlignment = NSTextAlignment.Left
textfield.placeholder = "textfield的使用"
textfield.secureTextEntry = false
<pre name="code" class="objc">// 光标颜色
textfield.textColor = UIColor.greenColor()
 
textfield.enabled = true
textfield.userInteractionEnabled = true
// 样式背景属性
textfield.backgroundColor = UIColor.lightGrayColor()
textfield.borderStyle = UITextBorderStyle.Line
// let image = UIImage(named: "normalImage")
// textfield.background = image
// 图标
let leftview = UIImageView(image: UIImage(named: "normalImage"))
leftview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
textfield.leftViewMode = UITextFieldViewMode.Always
textfield.leftView = leftview
let rightview = UIImageView(image: UIImage(named: "hightImage"))
rightview.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
textfield.rightViewMode = UITextFieldViewMode.Always
textfield.rightView = rightview
// 代理,注意添加代理协议,及实现代理方法
textfield.delegate = self
// 添加协议
class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
 
   }
}
// 代理方法
// MARK: - UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        
        print("1 textFieldShouldBeginEditing")
        
        return true
}
    
func textFieldDidBeginEditing(textField: UITextField) {
        print("2 textFieldDidBeginEditing")
}
    
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print("3 textField")
        
        return true
}

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        print("4 textFieldShouldEndEditing")
        print("text:\(textField.text) length = \(textField.text?.characters.count)")
        return true
}
    
func textFieldDidEndEditing(textField: UITextField) {
        print("5 textFieldDidEndEditing")
}
    
func textFieldShouldClear(textField: UITextField) -> Bool {
        
        print("6 textFieldShouldClear")
        
        return true
}
    
func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        // 结束编辑
        textField.resignFirstResponder()
        
        print("7 textFieldShouldReturn")
        
        return true
}
// 编辑属性设置
textfield.clearButtonMode = UITextFieldViewMode.WhileEditing
// 输入设置
textfield.keyboardType = UIKeyboardType.WebSearch
textfield.returnKeyType = UIReturnKeyType.Done
        
// 自定义输入源控件
// let inputview = UIButton(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 100.0))
// inputview.setImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)
// inputview.backgroundColor = UIColor.lightGrayColor()
// inputview.addTarget(self, action: Selector("click:"), forControlEvents: UIControlEvents.TouchUpInside)
// textfield.inputView = inputview
// 自定义输入源控件副视图
let accessoryview = UIView(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0))
accessoryview.backgroundColor = UIColor.greenColor()
let accessoryLeft = UIButton(frame: CGRectMake(10.0, 10.0, 60.0, 20.0))
accessoryview.addSubview(accessoryLeft)
accessoryLeft.setTitle("取消", forState: UIControlState.Normal)
accessoryLeft.backgroundColor = UIColor.orangeColor()
accessoryLeft.addTarget(self, action: Selector("leftClick:"), forControlEvents: UIControlEvents.TouchUpInside)
let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0), 10.0, 60.0, 20.0))
accessoryview.addSubview(accessoryRight)
accessoryRight.setTitle("确定", forState: UIControlState.Normal)
accessoryRight.backgroundColor = UIColor.orangeColor()
accessoryRight.addTarget(self, action: Selector("rightClick:"), forControlEvents: UIControlEvents.TouchUpInside)
textfield.inputAccessoryView = accessoryview
// 自定义输入源控件时响应事件
// MARK: - click
func click(button:UIButton)
{
        self.view.endEditing(true)
}
    
//MARK: - left/right click
func leftClick(button:UIButton)
{
        print("取消")
}
    
func rightClick(button:UIButton)
{
        self.view.endEditing(true)
        print("确定")
}
// 其他
// 第一响应,即进入编辑状态
// textfield.becomeFirstResponder()
// 结束响应,即结束编辑
// textfield.resignFirstResponder()
// 发通知-输入改变
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFiledEditChanged:"), name: UITextFieldTextDidChangeNotification, object: textfield)
// 通知响应方法-限制输入长度
// MARK: - 通知响应方法
func textFiledEditChanged(notification:NSNotification)
{
        let textField:UITextField! = notification.object as! UITextField
        if (textField != nil)
        {
            let text:String! = textField.text
            let length = text.characters.count
            if (length > 20)
            {
                textField.text = text.substringToIndex(text.startIndex.advancedBy(20))
            }
        }
}


注意:不能同时设置clearButtonModerightViewMode/rightView属性,否则只有rightViewMode/rightView有效。


 








源码:https://github.com/potato512/SYSwiftLearning


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值