How in iOS to localize text of the month in UIDate

2019-04-26 01:20发布

问题:

I make search and up to now I see that UIDatePicker is working with localization of the device.

I have app which has internal lozalization. I need to change month's text dependantly of my internal localization. As I read it appears that this is not possible - I have to make my own custom picker.

Is there a way to achieve that without custom Dat picker?

回答1:

Use locale property of UIDatePicker. I can't see any signs that the property is deprecated.

This code will localize picker to Japanese.

[datePicker setLocale: [NSLocale localeWithLocaleIdentifier:@"ja"]];

Replace "ja" with any language code you need.



回答2:

With Swift

let datePicker:UIDatePicker = UIDatePicker()
datePicker.locale = NSLocale.init(localeIdentifier: "it_IT")

Replace the it_IT string (for Italian-speaking people) with your locale identifier. Common cases for english are:

  • en for generic English-speaking people
  • en_US for English-speaking residents of the United States
  • en_GB for English-language speakers in the United Kingdom

For more information about the locale identifier see the Locale IDs section of "Internationalization and Localization Guide" by Apple

If you're interested in getting the current locale see How do I get current application locale?



回答3:

Swift 3.0 and New

This code will locale picker to Chinese. Here datePicker is object of UIDatePicker.

datePicker.locale = Locale.init(localeIdentifier: "zh-Hans")
  • Use "en" instead of "zh-Hans" for English .
  • Use "az-Arab" instead of "zh-Hans" for Arabic.
  • Use "uz-Latn" instead of "zh-Hans" for Latin.

for more : click here



回答4:

The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.

The locale property on UIDatePicker was deprecated in iOS 5, but you can try this:

NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:@"..."];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setLocale:loc];
[myDatePicker setCalendar:cal];


回答5:

UIDatePicker localize is dependent on the "Region Format" setting of device.

Settings > General > International > Region Format



回答6:

I think it is not possible with selecting UIDatepicker control may be you can do it with UIPickerview. please check this link. (you can do it like this way) UI appearance Datepicker
or Custom Color of Datepicker I hope it is helpfull to you.