简体   繁体   English

在IOS中输入时将占位符文本保留在UITextField中

[英]Keep placeholder text in UITextField on input in IOS

Is there any way to prevent placeholder text in a UITextField from being overwritten when the text field receives an input? 有什么方法可以防止文本字段收到输入时覆盖UITextField中的占位符文本? I have a text field that takes an expiration date in this format "MM/YYYY" with placeholder text that reads "MM/YYYY" and I want to remove the placeholder text as it is filled in, not when the first character is entered. 我有一个文本字段,其格式为“ MM / YYYY”的到期日期带有占位符文本,其内容为“ MM / YYYY”,我想删除填充的占位符文本,而不是在输入第一个字符时删除。

Example: "1M/YYYY" after first character "10/YYYY" after the second character etc. 示例:第一个字符后的“ 1M / YYYY”,第二个字符后的“ 10 / YYYY”等

Instead of using the placeholder text, you'll want to set the actual text property of the field to MM/YYYY, set the delegate of the text field and listen for this method: 您不希望使用占位符文本,而是将字段的实际text属性设置为MM / YYYY,设置文本字段的委托并侦听此方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // update the text of the label
}

Inside that method, you can figure out what the user has typed as they type, which will allow you to update the label accordingly. 在该方法内,您可以找出用户在键入时键入的内容,这将使您可以相应地更新标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM