目标 C 文本字段默认值“•••••••"到“1•••••••"、“12••••••"、“123•••••"当用户单击数字时 [英] Objective C Textfield Default Value “•••••••” to "1•••••••”,“12•••••”,“123••••” when user click number

查看:25
本文介绍了目标 C 文本字段默认值“•••••••"到“1•••••••"、“12••••••"、“123•••••"当用户单击数字时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create textfield that has default value "•••••••" , when user click a number on UIKeyboardTypeNumberPad, it will be added textfield like that "1••••••"(for example clicked number 1) and "12•••••"(for example second clicked number 2).Each click to UIKeyboardTypeNumberPad will be added to textfield respectively. How can i do this? Thanks.

推荐答案

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

I have used some trick to do this..

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

  1. First drag UILable and set text with ●●●●●●●.
  2. Now drag a UITextField and put it over UILable.
  3. Now change UITextField attributes in storyBoard like change UITextField TintColor as clearColor (Because i don't want to show blink cursor), textColor as clearColor (Because i don't want to show textfield text) and Keypad type NumberPad.
  4. UILable and UITextField textAlignment is center with same Font-Family and Font-Size.
  5. Now I add an IBAction i.e. Editing Changed to UITextField in my viewController from connection Inspector in storyBoard.
  6. Final step i.e. code inside Editing changed method--

-(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];
        }


    }

可能有更好的方法来做到这一点,但这是我的技巧,效果很好..谢谢

there is may be a better approch to do this but this my trick which works fine.. Thanks

这篇关于目标 C 文本字段默认值“•••••••"到“1•••••••"、“12••••••"、“123•••••"当用户单击数字时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆