Right now I'm working through Udacity's iOS Developer Nanodegree program. We're using Swift 2.0 and so far I'm really liking the program. I am currently working through the second course (UI Kit Fundamentals I) lesson 3 challenge app. This challenge app is working with UITextField Delegates. The challenge app consists of three textfields. The first textfield is a zip code field with a character limit of 5 and a numeric keyboard. The second field is a money field that contains a dollar sign at the left that always shows 2 places after the decimal. The text is filled in from the right and is appended onto the amount. So for example if you already entered $1.05 and then enter 3 the new value is $10.53. The final textfield is paired to a switch control. When the switch is on the textfield is locked and cannot be edited, when the switch is off then the textfield can be edited. I started working on the first textfield so that will be the focus of this post. The first thing I did was change the keyboard to numeric only.
To change the on screen keyboard first open up your storyboard and selected the text field. Next expand your Utilities Panel (if hidden) and go to the Attributes Inspector. From there you can change the Keyboard Type (left screen shot). Now there are a number of other options for keyboard type, as you can see in the right screenshot, but for this project I chose Number Pad.
In .Net all string objects are also character arrays. This was the case until Swift 2.0, read Apple String Reference before for more information about why that was changed. The String object in Swift 2.0 now exposes a characters property that is the characters array. When you're working with the UITextFieldDelegate the function includes the textfield object, the range and the incoming string. Since I wanted to limit the number of characters to 5 I first needed to get the number of characters in the string. In the first screenshot the text (the local variable text) property of the textfield would be "1234" with a character count of 4. Now if I enter 5 the string parameter will have a value of 5. So I can simple add the existing length (4) and the length of the new string (1) and check if the new length will be less than or equal to my limit of 5. Which it is in this case so the method returns true and the new string is added to the existing string. If I already had 5 characters then the method would return false and not allow the new string to be appended.
Now this is a very simple use case. I didn't even touch the range object and I didn't do any kind of string manipulation. I just limited the number of characters. One thing you will notice if you were to run this code in the simulator is that even though the on screen keyboard is showing only numbers you can use your physical keyboard to enter non numeric characters. Since the on screen keyboard is limited to numbers I decided not to worry about it. I hope this was helpful to you. Happy Coding.
Sean Wernimont The Blind Squirrel Copyright 2015-2020
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|