Objective C文本字段默认值“•••••••”到“1•••••••”,“12•••••”,“123••••”当用户点击编号时

我想创建具有默认值“•••••••”的文本字段,当用户单击UIKeyboardTypeNumberPad上的数字时,它将添加文本字段,如“1••••••”(例如,单击数字1)和“12•••••”(例如,第二次单击数字2)。每次单击UIKeyboardTypeNumberPad将分别添加到文本字段。 我怎样才能做到这一点? 谢谢。

我用了一些技巧来做到这一点..

  1. 首先拖动UILable并使用●●●●●●●设置文本。
  2. 现在拖动UITextField并将其放在UILable上。
  3. 现在改变storyBoard中的UITextField属性,比如改变UITextField TintColor为clearColor(因为我不想显示闪烁光标),textColor为clearColor(因为我不想显示文本字段文本)和键盘类型为NumberPad。
  4. UILable和UITextField textAlignment是具有相同Font-Family和Font-Size的中心。
  5. 现在我在storyBoard中的连接Inspector中添加了一个IBAction,即在我的viewController中更改为UITextField。
  6. 最后一步即内部代码编辑改变方法 –

    -(IBAction)textEditingChanged:(UITextField*)sender{ if(sender.text.length==0){ self.myLable.text=[NSString stringWithFormat:@"●●●●●●●"]; } if(sender.text.length==1){ self.myLable.text=[NSString stringWithFormat:@"%@●●●●●●",sender.text]; } if(sender.text.length==2){ self.myLable.text=[NSString stringWithFormat:@"%@●●●●●",sender.text]; } if(sender.text.length==3){ self.myLable.text=[NSString stringWithFormat:@"%@●●●●",sender.text]; } if(sender.text.length==4){ self.myLable.text=[NSString stringWithFormat:@"%@●●●",sender.text]; } if(sender.text.length==5){ self.myLable.text=[NSString stringWithFormat:@"%@●●",sender.text]; } if(sender.text.length==6){ self.myLable.text=[NSString stringWithFormat:@"%@●",sender.text]; } if(sender.text.length==7){ self.myLable.text=[NSString stringWithFormat:@"%@",sender.text]; [sender resignFirstResponder]; } } 

可能有一个更好的approch来做到这一点但这是我的诀窍工作正常..谢谢

在此处输入图像描述