Evg*_*nii 58
在iOS 9中,可以确定每个视图的当前方向.
if #available(iOS 9.0, *) {
if UIView.userInterfaceLayoutDirection(
for: myView.semanticContentAttribute) == .rightToLeft {
// The view is shown in right-to-left mode right now.
}
} else {
// Use the previous technique
if UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft {
// The app is in right-to-left mode
}
}
Run Code Online (Sandbox Code Playgroud)
这是在iOS 9中确定布局方向的推荐方法.
WWDC 2015视频新UIKit支持国际用户界面.分钟后31:20.
Acc*_*yyc 41
有一种官方方式可以做到:
if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
}
Run Code Online (Sandbox Code Playgroud)
我建议不要使用其他一些解决方案,因为它们不会总是返回正确的语言环境.仅仅因为它位于首选语言之上并不意味着应用程序支持它.
资料来源:https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html
Lil*_*ard 17
NSLocale有两种方法+characterDirectionForLanguage:和+lineDirectionForLanguage:.第一个可能是从左到右与从右到左,第二个是从上到下与从下到上.你可以传递它的结果[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode].
更新:
最初提出的问题是如何确定设备语言是否为RTL.使用+[NSLocale characterDirectionForLanguage:]和+[NSLocale lineDirectionForLanguage:]明确正确的; 您可以传递任何一个[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]或[NSLocale preferredLanguages][0]那个来获取相关信息(我不确定是否NSLocaleLanguageCode使用首选语言或设置区域).
然而,原始海报实际上想知道的很可能是应用程序的界面是否应该用RTL或LTR进行布局.这非常类似于询问语言的方向,除了它将应用程序的可用本地化考虑在内.如果应用程序未本地化为用户的首选语言,则它将使用非首选语言.这个问题的答案就是使用[UIApplication sharedApplication].userInterfaceLayoutDirection.
Ros*_*one 10
确保返回当前选定的语言,而不是设备的当前区域.区域和语言通常是相同的.但是,如果我在北美并将语言设为日语,我的地区仍将是英语(美国).要检查当前所选语言的字符方向,您可以执行以下操作:
+ (BOOL)isDeviceLanguageRTL {
return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
}
Run Code Online (Sandbox Code Playgroud)
您可能希望使用缓存结果dispatch_once.
请记住,这是用户首选的语言方向,而不一定是文本的语言方向.为此,请使用基于的C函数u_charDirection.
如果您只想知道 iOS 10+ 上的特定视图布局方向,您可以使用:
view.effectiveUserInterfaceLayoutDirection == .rightToLeft
Run Code Online (Sandbox Code Playgroud)
感谢 Kevin Ballard 的回答,我能够创建以下实用函数来执行此操作:
+ (BOOL)isDeviceLanguageRTL {
return [NSLocale characterDirectionForLanguage:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]]==NSLocaleLanguageDirectionRightToLeft;
}
Run Code Online (Sandbox Code Playgroud)
这是一个快速的3版本:
import UIKit
extension UIView
{
/// Returns text and UI direction based on current view settings
var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection
{
if #available(iOS 9.0, *) {
return UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute)
} else {
return UIApplication.shared.userInterfaceLayoutDirection
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18791 次 |
| 最近记录: |