views:

208

answers:

2

Hi

I have a shortage of screen real estate for my time labels in my iPhone app. My solution is to have the time e.g. 12:00 on one line and then if the users current locale specifies that an AM-PM is used, have these in a second label below it.

Since AM-PM also have localized variants I can't just look for the letters "AM" or "PM", then I thought about stripping the last two letters, but by checking I found out some languages uses a format like this: "F.M." "E.M". My next thought was to strip everything after the first 5 digits(12:34), but for hour intervals below 10 that is no good either.

Is there a "locale safe" way of always removing the localized suffix and move it to a new string, regardless of the users settings?

Thank you in advance:)

+1  A: 

There is no locale safe way of doing that.

Use NSDateFormatter to generate two strings.

NSDateFormatter  *timeOfDayFormatter = [[[NSDateFormatter alloc] init] retain];
[timeOfDayFormatter setDateFormat:@"hh:mm"];

NSDateFormatter  *amPmFormatter = [[[NSDateFormatter alloc] init] retain];
[amPmFormatter setDateFormat:@"aa"];


NSLog(@"Time is: %@ %@",
    [timeOfDayFormatter stringFromDate:theDate],
    [amPmFormatter stringFromDate:theDate]);

Now you can layout your user interface with the two strings.

The 24-hour format is standard where I live. I'd "force" that format on the user if screen real estate is a real problem.

Niels Castle
Hi Niels and thanks:)I already have an NSDate object and I could easily 'force' the 24 hour format on the user (would make the world a simpler place) but I would like respect their settings. When Im dealing with a user that has the 24 hour format, no problem. But when it is the 12 hour format I need to raise a flag and strip the "AM" or "PM" suffix from the string and move that to a separate UILabel.The app deals with appointments, so time and dates are an important thing and I don't wish users to feel uncertain or doubtful about a time (as I do when dealing with 12 hour clocks :) )
RickiG
I'd either go with the 24 hour notation or full AM/PM notation. I don't think you can respect the users locale *and* mangle the display format. Perhaps there is some other way to inform the user that appointments are upcoming... "In 10 min", red flag, clock icon, or something similar? Sun or moon icon?
Niels Castle
Hi Niels.This is more of an NSString question than an NSDate question.I use the NSDateFormatterShortStyle for the date. convert it to an NSString. If this string contains a 12 hours clock suffix, move this suffix to a label beneath the time label. It is also good for other things. e.g. the AM PM suffix of course get's the same font size as the label it is put into. by removing it and displaying it in a separate label I can also set the size to something smaller that the time label.Thanks again.
RickiG
Ah, then use two formatters one for the time and one for the suffix (what ever that would be in the relevant locale). You could even layout the AM/PM as an sub or super script to the time. As in 1^2. Good luck with your application!
Niels Castle
Hi Niels.I actually have a different place in the app where I need the suffix to be after the time, but where I found the size of AM/PM to be way to much, here subscript would be perfect. Thanks for mentioning that one:)
RickiG
A: 

I ended up solving it like this and then manually test it with 15+ locale settings:

NSArray *timeParts = [NSArray arrayWithArray:[[timeFormatter stringFromDate:myDate] componentsSeparatedByString:@" "]];

Then I test the timeParts array:

if ([timeParts count] > 1) {...}

If the count is 1 it is a locale without the suffix and I don't set the "AM/PM" label. Else, I set both labels, the timeLable with [timeParts objectAtIndex:0] and the localeLabel with [timeParts objectAtIndex:1]

This seems to be a stable solution for all locales.

RickiG