uitextfield

How to restrict UITextField to take only numbers in Swift?

百般思念 提交于 2019-12-17 15:21:55
问题 I want the user to only enter numeric values in a UITextField . On iPhone we can show the numeric keyboard, but on iPad the user can switch to any keyboard. Is there any way to restrict user to enter only numeric values in a UITextField ? 回答1: Here is my 2 Cents. (Tested on Swift 2 Only) func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let aSet = NSCharacterSet(charactersInString:"0123456789").invertedSet let

How to Test Character Input to UITextField as the User Enters Characters and Prevent Invalid Characters

半世苍凉 提交于 2019-12-17 10:29:40
问题 First, I setup up the keyboard for the UITextField to use the number with decimal style. So the user can only enter numbers and a single decimal. What I want to do is test the input as the user enters it and prevent multiple decimals from being entered and limit the decimal portion of the number to two places. I do not want to round off the number nor even treat the input as a number. I simply want to prevent the user from entering more then two digits to the right of the decimal place. 回答1:

Swift: retrieving text from a UITextField in a custom UITableViewCell and putting it in an array

Deadly 提交于 2019-12-17 09:49:30
问题 I'm making a very simple app where the user enters the number of people in the first Screen. In the second screen it generates a number of UITableViewCell based on the number the user entered in the first screen. The UITableViewCell have a UITextField in them and I'm trying to store the data entered in those fields in an array once the user clicks to go to the third screen. How can I do that? Thanks in advance! Edit: I'm using the storyboard. Here is what the code that calls for the custom

UITextField in UIAlertController (border, backgroundColor)

感情迁移 提交于 2019-12-17 09:47:55
问题 Here is a screenshot of a UIAlertController . I was just playing around custom fonts and textfield properties but I was unable to accomplish the following: clear background of the UITextField no ugly border (black box) as shown below As I dived more into the code and iOS runtime headers, I was able to modify border and background color but the above issue still remains as those properties belong to a container UITextView . Changing background to clearColor doesn't help. Has anybody ever

How can I limit the number of decimal points in a UITextField?

ぐ巨炮叔叔 提交于 2019-12-17 09:21:11
问题 I have a UITextField that when clicked brings up a number pad with a decimal point in the bottom left. I am trying to limit the field so that a user can only place 1 decimal mark e.g. 2.5 OK 2..5 NOT OK 回答1: Implement the shouldChangeCharactersInRange method like this: // Only allow one decimal point // Example assumes ARC - Implement proper memory management if not using. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *

How to change the tint color of the clear button on a UITextField

佐手、 提交于 2019-12-17 08:29:56
问题 I have an auto-generated clear button on my UITextfield, with the default blue tint color. I cannot change the tint color to white. I have tried modifying the storyboard and code without success, and I do not want to use a custom image. How can I change the default clear button tint color without using a custom image? 回答1: Here you go! A TintTextField. Using no custom image, or added buttons etc. class TintTextField: UITextField { var tintedClearImage: UIImage? required init(coder aDecoder:

UITextField with secure entry, always getting cleared before editing

走远了吗. 提交于 2019-12-17 08:25:09
问题 I am having a weird issue in which my UITextField which holds a secure entry is always getting cleared when I try to edit it. I added 3 characters to the field, goes to another field and comes back, the cursor is in 4th character position, but when I try to add another character, the whole text in the field gets cleared by the new character. I have 'Clears when editing begins' unchecked in the nib. So what would be the issue? If I remove the secure entry property, everything is working fine,

How to dismiss keyboard iOS programmatically when pressing return

孤者浪人 提交于 2019-12-17 08:09:48
问题 I created a UITextField programmatically making the UITextField a property of the viewController. I need to dismiss the keyboard with the return and the touch on the screen. I was able to get the screen touch to dismiss, but pressing return is not working. I've seen how to do it with storyboards and by allocating and initializing the UITextField object directly without creating it as a property. Possible to do? .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController

Re-Apply currency formatting to a UITextField on a change event

可紊 提交于 2019-12-17 06:13:19
问题 I'm working with a UITextField that holds a localized currency value. I've seen lots of posts on how to work with this, but my question is: how do I re-apply currency formatting to the UITextField after every key press? I know that I can set up and use a currency formatter with: NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; ... [currencyFormatter stringFromNumber:...]; but I don't know how to hook it

Display datepicker on tapping on textfield

为君一笑 提交于 2019-12-17 04:52:07
问题 How can I display a datetimepicker control on tap of a textbox? I have a user interface that has arrival and departure text fields, and when a user clicks on arrival textbox it should bring up a datetimepicker control up instead of a keyboard and the same with the departure textbox. 回答1: You can use inputView and inputAccessoryView properties of the text field for this. Create the date picker and set it to the input views of the two text fields. Also create another view for the Done button