问题
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..
- First drag UILable and set text with ●●●●●●●.
- Now drag a UITextField and put it over UILable.
- 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.
- UILable and UITextField textAlignment is center with same Font-Family and Font-Size.
- Now I add an IBAction i.e. Editing Changed to UITextField in my viewController from connection Inspector in storyBoard.
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