Objective C Textfield Default Value “•••••••” to "1•••••••”,“12•••••”,“123••••” when user click number

拈花ヽ惹草 提交于 2019-12-10 23:55:56

问题


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.


回答1:


I have used some trick to do this..

  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



来源:https://stackoverflow.com/questions/36768405/objective-c-textfield-default-value-to-1-12-123

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!