iOS: Determine if device language is Right to Left (RTL)

前端 未结 11 1852
后悔当初
后悔当初 2020-12-04 23:20

Is there a way to easily determine if the language the device is set to is right to left (RTL)?

11条回答
  •  感情败类
    2020-12-05 00:02

    Make sure you return the currently selected language, not the current region of the device. The region and language are often the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to check the character direction of the currently selected language, you can do:

    + (BOOL)isDeviceLanguageRTL {
      return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
    }
    

    You may likely want to cache the result, using dispatch_once.

    Keep in mind that this is the user's preferred language direction, and not necessarily the language direction of the text. For that, use a C function that is based on u_charDirection.

提交回复
热议问题