nsformatter

UITextField shouldChangeCharactersInRange doesn't detect first character

余生颓废 提交于 2019-12-11 13:55:13
问题 I have a UITextField and I am trying to format it as phone number, however I am struggling getting the first letter of the UITextField for some reason. func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if textField == myTextField { var updatedTextString : NSString = PartialFormatter().formatPartial(textField.text!) self.formattedPhoneNumber(updatedTextString, textField: textField) } return true } func

Thoughts in accessing read only objects from different threads

天大地大妈咪最大 提交于 2019-12-10 09:25:46
问题 Based on a previous discussion I had in SO (see Doubts on concurrency with objects that can be used multiple times like formatters), here I'm asking a more theoretical question about objects that during the application lifetime are created once (and never modified, hence read-only) and they can be accessed from different threads. A simple use case it's the Core Data one. Formatters can be used in different threads (main thread, importing thread, etc.). NSFormatter s, for example, are

Formatting UITextField's text as Phone Number as user types in

梦想与她 提交于 2019-12-09 20:29:45
问题 I have a custom UITextField, which I am trying to 'format the input to phone number' as user types. What I want to achieve: Adding a prefixed + sign on the UITextField as the first character, that cannot be deleted. Format both the String variable and UITextField's text in the format of phone number nicely (+49 291 12345678) , rather than plain numbers (4929112345678) as user types in. I researched and found out that there is no built-in method for that. I've also found easy-to-use library

How to round off decimal number to 2 digits in iphone?

穿精又带淫゛_ 提交于 2019-12-08 17:25:04
问题 How can we round off decimal numbers to 2 digits in iphone ? For example , 5345 --> 53 570 -- > 57 89523 -- > 90 I tried to use nsformatter for this and here is the code i used , NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setMaximumIntegerDigits:2]; [formatter setRoundingMode: NSNumberFormatterRoundUp]; NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithInt:570]]; [formatter

How do you specify the locale/language for an NSFormatter that doesn't have a locale property?

妖精的绣舞 提交于 2019-12-07 10:54:14
问题 There are a lot of specialized subclasses of NSFormatter: CNContactFormatter CNPostalAddressFormatter DRMSFFormatter MKDistanceFormatter NSByteCountFormatter NSDateComponentsFormatter NSDateFormatter NSDateIntervalFormatter NSEnergyFormatter NSLengthFormatter NSMassFormatter NSNumberFormatter NSPersonNameComponentsFormatter Most of us have used NSNumberFormatter and NSDateFormatter . Those have a locale property that lets you specify what locale to use when converting from strings to/from the

How do you specify the locale/language for an NSFormatter that doesn't have a locale property?

烂漫一生 提交于 2019-12-05 16:54:39
There are a lot of specialized subclasses of NSFormatter: CNContactFormatter CNPostalAddressFormatter DRMSFFormatter MKDistanceFormatter NSByteCountFormatter NSDateComponentsFormatter NSDateFormatter NSDateIntervalFormatter NSEnergyFormatter NSLengthFormatter NSMassFormatter NSNumberFormatter NSPersonNameComponentsFormatter Most of us have used NSNumberFormatter and NSDateFormatter . Those have a locale property that lets you specify what locale to use when converting from strings to/from the "native" format the formatter supports. Some others like NSDateComponentsFormatter inherit their

Cursor shifts to end on edit of formatted decimal textfield - Swift

别来无恙 提交于 2019-12-01 20:24:18
问题 I am formatting a UITextField such that it becomes comma separated (Decimal style) while typing. So 12345678 becomes 12,345,678 Now when I edit the UITextField , say I want to remove 5 , at that time I tap after 5 and delete it but the cursor shifts to the end of the text immediately, that's after 8 . Here's my code which I have used to format the decimal while typing: func checkTextField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) ->

dateFromString return nil after change 24 hour

北战南征 提交于 2019-12-01 09:50:42
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"hh:mm a"]; NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; if(sourceDate==nil) { NSLog(@"source date nil"); } I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ? The locale needs to be set to avoid being affected by the 24-hour setting change. The format also has to match the input string (which in your example includes the date): NSDateFormatter *timeFormat = [