UIPickerView Changing UIButton Title Size - xcode

So I'm making Alarm Clock to test my skills but can't seem to figure this out... What I've got a UIPickerView that gives a user a time that they can select. Once the time is selected the titleLabel on a UIButton is supposed to update with the time they selected and it does, but it shrinks the new time so it's unreadable... Is there something that needs adjusted with the formatting?
Before when my page loads
Here's my code
- (NSDate *)dateFromString:(NSString *)string {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"15:00"];
assert(date != nil); // Error converting string to date
return date;
}
After when a user has set a time
Any reason why it might be doing this?

You're experiencing middle truncation of the button's text, and there are a couple of cures for this. You can either make the button bigger by simply adjusting its frame. Or, you can adjust the text size of the title label:
[myButton.titleLabel setFont:[UIFont fontWithName:myButton.titleLabel.font.fontName size:10.0f]];

Actually this seemed to resolve the issue.
alarmButton.titleLabel.adjustsFontSizeToFitWidth = YES;

Related

How to calculate and display 4 other days from the day i add in date picker and set local notification to it?

In my App, When i select a day in the datepicker it should automatically calculate and display 4 other days and also it should notify on the corresponding days. For Eg. if i enter 19-08-2014, it should calculate and display the 3rd day, the 7th day, the 14th day and the 21st day from 19-08-2014(the day i entered in datepicker).
How would i achieve this. Any help would be greatly appreciated.
I have added my current code for your reference. This doesnt serve my purpose.
Kindly Help.
(IBAction)save:(UIButton *)sender {
NSDate *pickerDate = [self.picker date];
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
//localNotif.alertBody = _enterText.text;
localNotif.alertBody = #"Please Take Your Rabipur Dosage";
localNotif.fireDate = pickerDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.soundName = (UILocalNotificationDefaultSoundName);
localNotif.applicationIconBadgeNumber = 1;
//localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
[self dismissViewControllerAnimated:YES completion:nil];
}
Create an NSDateComponents object with the date difference you want to add, and add it to pickerDate via NSCalendar -dateByAddingComponents:toDate:options. Then create your notification based on the resulting date.
In this case, something like this:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:3];
NSDate *reminderDate = [gregorian dateByAddingComponents:offsetComponents toDate:pickerDate options:0];
Repeat for 7th, 14th, and 21st day.

Call phone number from UIButton

I've searched through all the questions and can't seem to find my answer.
I have the following IBAction. This is crashing every time you tap on the phone number. I have gone back to the DB and formatted the phone numbers to 5551235555 instead of (555)-123-5555.
- (IBAction)callPhone:(UIButton *)sender{
Bar *items = self.detailItem;
NSURL *pn = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", items.barPhone]];
[[UIApplication sharedApplication] openURL:pn];
}
- (void)setCallButton:(UIButton *)callButton{
Bar *items = self.detailItem;
[callButton setTitle:items.barPhone
forState:UIControlStateNormal];
}
Any code guidance would be appriciated.
Bar *items = self.detailItem; is not initiated, so that is why it is returning nil. Try the following:
Bar *items = [[Bar alloc] init];
items = self.detailItem;
Or what you should have done is make items an ivar for this particular class. Then you can initiate items once and use it throughout your class.

cocoa: speech and time

I'm building an app that a part of will speak the time. However, when I pass in my date strings (like 10/24/11) to the NSSpeechSynthesizer it will speak them literally, as "one, zero, slash two four slash one one", same with a timestamp, "eight colon one one colon colon", etc. etc.
I looked at the NSSpeechSynthesizer docs and I guess I'd have to work with the phonemesFromText method but that seems like a lot of grunt work to get the app to speak the time and date smoothly. Is there a quicker method?
Thanks
You could try something like this:
#implementation MDAppController
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *dateParser = [[[NSDateFormatter alloc]
initWithDateFormat:#"%m/%d/%y" allowNaturalLanguage:YES] autorelease];
NSDate *date = [dateParser dateFromString:#"10/24/11"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *string = [dateFormatter stringFromDate:date];
NSLog(#"string == %#", string);
// prints "October 24, 2011"
NSSpeechSynthesizer *alex = [[NSSpeechSynthesizer alloc]
initWithVoice:[NSSpeechSynthesizer defaultVoice]];
[alex setDelegate:self];
[alex startSpeakingString:string];
}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender
didFinishSpeaking:(BOOL)finishedSpeaking {
if (finishedSpeaking) [sender autorelease];
}
#end
Basically, this is using 2 NSDateFormatters: one to "translate" the string representation of a date into an actual NSDate object, and then another to translate that NSDate back into a more desirable string representation.
Obviously, you'll need to adjust the dateParser format to fit your expected input string type. (Preferably, you could just use an input date rather than the string representation of it).
Why not use NSDateComponents to and -[NSString stringWithFormat:] construct a spoken sentence as your string, then speak that?

Time comparison in Cocoa

I am storing the date and time in strings using NSUserDefaults when an action is performed. next time the app is run, I want to check the time since the last date and time, and display a message if this is greater than a specified time period.
Is this possible?
In viewDidLoad I retrieve the NSUserDefaults with the saved date and time, and I get the current date, but how do i compare them, and 'do something' if the time difference is bigger than specified?
Use NSDate's timeIntervalSinceDate: method.
By the way, you can store NSDates directly in NSUserDefaults. You don't need to convert them to/from strings.
You could do something like that:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate * lastUpdate = [defaults valueForKey:#"mysettingdate"];
if(!lastUpdate){
lastUpdate = [NSDate dateWithTimeIntervalSince1970:0];
}
NSDate *localDate = [NSDate date];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] + timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSDateComponents *diff = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:lastUpdate toDate:gmtDate options:0];
NSLog(#"diff in minutes:%d", [diff minute]); //or hour, days, seconds
if ([diff minute]>0) {
//do your stuff :)
}

Countdown Timer in Cocoa

I am wondering if there is some way that I can create a timer that countdown from a given time. For example, say I want this timer to last an hour. There will be a NSTextField that will show the time remaining (ex. 25 minutes), and will auto update every minute to the new value. And then, when an hour is finally passed, it will run some function. I have seen people suggesting NSTimer and NSDate for this, but am wondering what you all could suggest.
Thanks!
EDIT: My current code (timeInstance is an instance variable):
- (void)awakeFromNib:
{
timeInstance = [[NSDate date] addTimeInterval:(10 * 60)];
[timeInstance retain];
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(timer:) userInfo:NULL repeats:YES];
}
- (void)timer:(NSTimer *)myTimer
{
NSDate *now = [NSDate date];
// Compare
}
NSTimer and NSDate sounds perfectly reasonable.
EDIT: As a side note, it might be a good idea to increase the frequency as the target time approaches; allowing you to change from hour display to minute display to second display.

Resources