Jump to content
Chris Pim

TTimeEdit picker time format

Recommended Posts

Hi everyone

 

Does anyone know if it's possible to force the time picker of TTimeEdit to be 12 or 24 hours?

I mean the actual picker in iOS itself, not the editor box (which you can manipulate using the "Format" property).

 

By default, the picker matches the time format set in the phone settings, but I have a separate time format setting in my app which determines whether we show am/pm or 24 hour format in the app and I'm trying to make the experience consistent for my users.

 

Thanks

Share this post


Link to post
7 hours ago, Chris Pim said:

Does anyone know if it's possible to force the time picker of TTimeEdit to be 12 or 24 hours?

Since you mention iOS:

uses
  Macapi.ObjCRuntime, Macapi.Helpers,
  iOSapi.UIKit, iOSapi.Helpers, iOSapi.Foundation;

function FindUIDatePicker(const AView: UIView; out ADatePicker: UIDatePicker): Boolean;
var
  I: Integer;
  LSubView: UIView;
begin
  Result := False;
  if AView.subviews.count > 0 then
  begin
    for I := 0 to AView.subviews.count - 1 do
    begin
      LSubView := TUIView.Wrap(AView.subviews.objectAtIndex(I));
      if LSubView.isKindOfClass(objc_getClass('UIDatePicker')) then
      begin
        Result := True;
        ADatePicker := TUIDatePicker.Wrap(AView.subviews.objectAtIndex(I));
        Break;
      end
      else if FindUIDatePicker(LSubView, ADatePicker) then
      begin
        Result := True;
        Break;
      end;
    end;
  end;
end;

function GetLocale(const AIs24Hour: Boolean): NSLocale;
var
  LIdent: NSString;
begin
  if AIs24Hour then
    LIdent := StrToNSStr('en_GB') // I say, old chap
  else
    LIdent := StrToNSStr('en_US'); // Yeehaw!
  Result := TNSLocale.Wrap(TNSLocale.OCClass.localeWithLocaleIdentifier(LIdent));
end;

procedure TForm1.TimeEdit1OpenPicker(Sender: TObject);
var
  LDatePicker: UIDatePicker;
begin
  if FindUIDatePicker(TiOSHelper.SharedApplication.keyWindow.rootViewController.view, LDatePicker) then
    LDatePicker.setLocale(GetLocale(True)); // or False, for 12 hour
  // else it was not found
end;

FindUIDatePicker iterates the native view hierarchy looking for the UIDatePicker. The alternative would have been to "hack into" FMX.Pickers.iOS. 

 

Note: By default, the locale property of UIDatePicker is nil, which means it uses whatever the device setting is (Settings > General > Date & Time). If a locale is assigned, it uses the default time mode for that locale. As long as the default time mode does not change for the two used in the code, it should remain working.

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×