Formatting UITextField for time - ios

I want to make a single UITextField that displays the format 00:00.00 where the : and . are permanently set. So the user will tap the text field to edit it, and instead of the textfield going blank, I would like it to be __:__.__ (the underscores are blanks) and the user can type two numbers for the minutes, automatically shift to the seconds, type two numbers, and then autoshift to the milliseconds. How can this be done?

Alternatively, you could just use a UIDatePicker:
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:self.view.frame];
[self.view addSubview:picker];
This is also available for drag-and-drop in the storyboard.

Related

UIDatePicker Shows Empty Picker view

UIDatePicker Shows Empty Picker View. It's working perfect, I mean It is scrolling, It is setting date to respective textfield but in picker view it is not displaying date. I have attached a screenshot.
Code for DatePiker,
datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setDate:[NSDate date]];
_txtDue.inputView = datePicker;
Any help will be appreciated!
Finally I figure out it. It was a issue of one third party library called UIHidingView!! It was added by someone in this project. This library's description from github states that,
UIHidingView is an iOS class that displays UIView element on top UITableView which is hiding when Table View is scrolling
Now UIDatePicker internally used tableview in it so, it was hiding it's components!
PS : take care when using third party libraries!!

Is there a way to make the iOS default keyboard show the numbers version first? [duplicate]

Created a UITextField for "Postal Code/ZIP" field with a keyboardType of UIKeyboardTypeDefault. I would like to use the default keyboard but want the numbers & symbols to be shown by default as apposed to letters.
Apple does this when you are entering addresses in the Contacts.app.
Anyone know how to get this accomplished?
You want the UIKeyboardTypeNumbersAndPunctuation keyboard type.
And a code example of what executor21 said:
UITextField* txt = [[UITextField alloc] init];
txt.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

Setting custom font for UILabel appearance causes UIDatePicker bug

In my app delegate I set the font and fontColor of all UILabels using [UILabel apperance], but this is causing the font in my UIDatePicker to also change, which Apple don't allow, and for obvious reasons because it makes the datePicker buggy by changing to default font while scrolling and other inconsistent and unintended behaviour.
How do I ignore UIDatePicker to keep it default when setting all UILabels?
I haven't found a "proper" solution for this problem short of abandoning UIDatePicker or [UILabel appearance], but I did figure out a hack that at least hides the problem. It seems that when the view is initially loaded, the UILabels holding the text for the selected date may be drawn using the label's appearance proxy, but when the UIDatePicker redraws after having the date reset, it will set the label back to the system font.
To hide the problem, in viewWillAppear:, I set the date to two different date values with different date components, then set the picker back to the proper date, like so:
[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:0] animated:NO]; //1970-01-01
[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:49000000] animated:NO]; //1971-07-22
[datePicker setDate:myDate animated:NO]; //Set back to the correct date
It's not elegant, but it makes the date picker look normal again.
The only simple work-around I found was to create the UIDatePicker in code (not using storyboards). I got what I wanted.
Try this ..
[[UILabel appearance] setTextColor:[UIColor redColor]];
[[UILabel appearance] setFont:[UIFont boldSystemFontOfSize:20.0f]];
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setTextColor:nil];// default is nil (text draws black)
[[UILabel appearanceWhenContainedIn:[UIDatePicker class],nil] setFont:nil];// default is nil (system font 17 plain)
This will change all labels to red colour and their font except the UIDatePicker one. Hope this helps.
Red colour and bold font are for demonstration purposes. They should be as per your requirement.

Access UIButtons from the interface

Background: I am creating a music app that gets data from my piano keyboard through CoreMIDI. When a note is played, it relays this to the GUI by showing the onscreen keyboard key as being pressed. The onscreen keyboard is represented by UIButtons for now. Each "key" (UIButton) has an identifier that corresponds to the MIDI value from my physical piano keyboard. For instance, middle C's UIButton will have a value of 60, since that is the value returned in MIDI data. C# is 61, D is 62, etc.
Question: How can I access all of the UIButtons from my window programatically? I need to access them by their "identifier" field, unless there's a more elegant way to do so. I haven't been programming for OSX for very long (actually only a few days) and I've literally watched about 100 videos on YouTube so I'm still learning. I have read a few posts that have said each UIButton is a subview of the main view. I couldn't figure out how to access the buttons via code though. Whatever suggestions you have will be very helpful! Thanks in advance.
Assuming you have all the buttons declared as outlets in your controller and connected them with your view.
Beware: Lots of untested pseudo-code will follow ;-)
#property IBOutlet UIButton *firstPianoKeyButton;
#property IBOutlet UIButton *secondPianoKeyButton;
#property IBOutlet UIButton *thirdPianoKeyButton;
... and so on ...
What you can do now is, to put the buttons one after the other into an array.
Let's say you have a controller with 12 keys, starting with what would usually be the middle C.
NSArray *pianoKeys = [NSArray arrayWithObjects:firstButton, secondButton, thirdButton, ..., nil];
Not nice, and there are more elegant and efficient ways to do this, but that should give the picture.
More on arrays in general: NSArray.
Check out this video here for a "better" way: Create Multiple UIButtons Programmatically. It's for iOS though, but again, it should give the picture :-D
Although check out this one: How do I create a basic UIButton programmatically?
Okay. Now, your controller will receive for example the NoteOn-Event of the D#, that corresponds to the MIDI note number of 63. Assuming this is an integer, we can use that one to access the corresponding element in our pianoKeys-array.
So we need an offset to get the right index, the offset will be 60. Why? Our array starts with index 0, but our note numbers of the 12 key piano start with 60.
On a full 88-key piano, the first MIDI note number starts with 21, so that would be the index in that case.
Now, that we can calculate the array index with the MIDI note number of the pressed key and can therefore display the corresponding button in the view as pressed.
[pianoKeys objectAtIndex:(63-60)];
Hope this is helpful.
This is the answer for my second problem where the button could only be clicked in the middle:
Here was the code (in awakeFromNib):
WhitePianoKey *button = [[WhitePianoKey alloc] initWithFrame:NSMakeRect(20, 20, 40, 300)];
[[window contentView] addSubview: button];
[button setButtonType:NSMomentaryPushInButton]; //I removed this line
changed to
WhitePianoKey *button = [[WhitePianoKey alloc] initWithFrame:NSMakeRect(20, 20, 40, 300)];
[[window contentView] addSubview: button];
It seems that setting the button type overrides my subclass and sets the size of the button back to the default size of whatever the style of the button is that I changed it to.
Thanks a lot again man!

How do I programmatically switch keyboard layouts on the iPad?

I am having trouble finding resources on this, it may be a keyword thing. I am developing for the iPad, and I have text fields with numbers. I understand there is not a dedicated numeric keyboard, I wish to know if there is a way after the keyboard shows to programmatically switch to the numeric view.
I have seen other apps do this, so I don't think it is a matter of 'if' as much as 'how'. How do I programmatically switch keyboard layouts on the iPad?
Set your text field's keyboardType property to UIKeyboardTypeNumberPad or UIKeyboardTypeNumbersAndPunctuation. I think on an iPad they will currently both give you the numeric+punctuation keyboard, but perhaps a future version of iOS will have a numeric only keyboard, so you should use request one if it's more appropriate.
If you are creating the text field programatically:
UITextField *numericTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 185, 30)];
numericTextField.adjustsFontSizeToFitWidth = YES;
numericTextField.keyboardType = UIKeyboardTypeNumberPad;
[parentView addSubview:numericTextField];
Or if you're using interface builder, there is a setting in the inspector pane to control the keyboard type.

Resources