简体   繁体   English

UILabel和UITextField编辑字符

[英]UILabel and UITextField edit character

I m doing a anagram type of task in iOS. 我正在iOS中执行类似字谜的任务。 In this case i need a solution, 在这种情况下,我需要一个解决方案,

For ex: 例如:

NSString* quest = @"SATCK";
labelQuestion.text = quest;

I have a UITextField to type the answer. 我有一个UITextField可以键入答案。 The correct answer is "STACK". 正确的答案是“堆栈”。 The user will type this to the UITextFiled. 用户将其键入UITextFiled。 For ex: When the user types "S" in the textfield i need to remove the character "S" from the UILabel, similarly if he deletes the character "S" in textField I need to bring back the "S" in UILabel. 例如:当用户在文本字段中键入“ S”时,我需要从UILabel中删除字符“ S”,类似地,如果他删除了textField中的字符“ S”,则需要在UILabel中重新显示“ S”。 This should happen for all the characters user types. 所有字符用户类型都应该发生这种情况。 If the user types "STACK" in textField i need to hide S, T, A, C, K characters from the UILabel one by one. 如果用户在textField中键入“ STACK”,则需要一一隐藏UILabel中的S,T,A,C,K个字符。 HOw can I achieve this. 我该如何做到这一点。

Thanks 谢谢

You need to set a delegate object that conforms to the " UITextFieldDelegate " protocol. 您需要设置一个符合“ UITextFieldDelegate ”协议的委托对象。

Then you can catch characters being typed into your text field via [UITextFieldDelegate textField:shouldChangeCharactersInRange:replacementString:] . 然后,您可以通过[UITextFieldDelegate textField:shouldChangeCharactersInRange:replacementString:]捕获在文本字段中键入的字符。

In that method in your delegate object, you can write code to detect what characters are being typed in and you can subtract those letters from your label (or re-add them if the characters were deleted from the text field). 在委托对象的该方法中,您可以编写代码以检测要键入的字符,还可以从标签中减去这些字母(如果从文本字段中删除了这些字符,则可以重新添加它们)。

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

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