将文本添加到UITextField您不能删除 [英] Adding text to UITextField you can't delete
本文介绍了将文本添加到UITextField您不能删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是在 UITextField
开头添加一些文本,无法删除。当用户点击 UITextField
时,添加以下内容:
What I would like to do is to add some text at the beginning of UITextField
that cannot be deleted. I add this text when the user taps on UITextField
like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.text.length == 0) {
textField.text = @"myText";
}
return YES;
}
如何防止用户以正常方式删除此文本? ( 不需要在TextField的顶部添加一个单独的 UILabel
)。
提前感谢!
How do I prevent the user from deleting this text in a normal way? (without adding a separate UILabel
on top of the TextField, for example).
Thanks in advance!
推荐答案
您可以在中实现此代码shouldChangeCharactersInRange
delegate方法。
You can implement this code in shouldChangeCharactersInRange
delegate method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length == 1 && [string isEqualToString:@""]) {//When detect backspace when have one character.
textField.text = @"myText";
}
return YES;
}
这篇关于将文本添加到UITextField您不能删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文