localizing + customizing the date format of a UIDatePicker

ぃ、小莉子 提交于 2020-01-15 06:50:47

问题


I need to customize and localize the date format of a UIDatePicker's DateAndTime mode, so that it will display the date numbers in Arabic language plus time in 24 hours format and in Arabic numbers too.

the equivalent row in English would be: 2017 11 15 17:00

I've tried all the solutions proposed on SO from changing the locale to changing device language but to no avail.


回答1:


Let's breaking it down:

  • For displaying the content of the date picker to be in Arabic language you'd need to set it your desired locale:

The locale used by the date picker.

  • For changing the format of the date picker, well... it depends on the used locale; As mentioned in the UIDatePicker - Internationalization:

Date pickers handle their own internationalization; the only thing you need to do is specify the appropriate locale. You can choose a specific locale for your date picker to appear in by setting the Locale (locale) field in Attributes Inspector. This changes the language that the date picker uses for display, but also the format of the date and time (for example, certain locales present days before month names, or prefer a 24-hour clock over a 12-hour clock)...

which means that setting the desired locale for changing the language also affects the format date of the date picker.

Roughly speaking, setting both language and format is unavailable for your case, you should choose the desired language or the desired date format.

However, applying the first point (changing the date picker locale):

datePicker.locale = Locale(identifier: "ar")

should leads to the following output:

Also, the following question:

Show Time in 12 and 24 hour format in UIDatePicker on the basis of app settings not on device Settings

is also related to changing the date picker format.



来源:https://stackoverflow.com/questions/47049344/localizing-customizing-the-date-format-of-a-uidatepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!