Supporting Right-to-Left Languages
For the most part iOS supports Right-to-Left (RTL) languages such as Arabic with minimal developer effort. Standard UIKit controls take care of switching text alignment and direction automatically as long as you follow some simple guidelines.
Handling exceptions to this natural layout has not been so easy. For example, having right-aligned text switch to the left with a right-to-left language. After a quick recap on natural text alignment I look at how to fix this with the new semantic content API added in iOS 9.
Natural Text Alignment - A Recap
There are two simple guidelines you need to follow to have UIKit automatically adjust layout for right-to-left languages:
- Use leading/trailing auto layout constraints, not left/right constraints
- Use
NSTextAlignmentNatural(.Natural) notNSTextAlignmentLeft(.Left)
To test RTL text support I have three UILabel objects with horizontal Auto Layout constraints from the leading edge of the label to the leading margin. I will not bother to describe the vertical constraints.

Leading constraints
If we inspect the leading constraint of the first label the menu for each of the items should have Respect language direction ticked:

This can be confusing but is just choosing between having a constraint using leading rather than left edges. For comparison, here is how we would create this constraint in code:
NSLayoutConstraint(item: yesterdayLabel,
attribute: .Leading,
relatedBy: .Equal,
toItem: view,
attribute: .LeadingMargin,
multiplier: 1.0,
constant: 0.0).active = true
If you untick Respect language direction the constraint switches to using the left edge and left margin:

In code this constraint would now be like this:
NSLayoutConstraint(item: yesterdayLabel,
attribute: .Left,
relatedBy: .Equal,
toItem: view,
attribute: .LeftMargin,
multiplier: 1.0,
constant: 0.0).active = true
Remember that to support right-to-left languages use leading/trailing not left/right constraints.
Natural Text Alignment
The second point we need to check is the text alignment. If we inspect the UILabel you should see we are using the alignment labelled ---:

Xcode does not make it obvious but this is the natural alignment which means the label will use the default alignment for the application language. If you wanted to set it in code:
yesterdayLabel.textAlignment = .Natural
Interface Builder defaults mean there is nothing extra to do for basic right-to-left text support.
Simulating Right-To-Left Languages
If you are yet to localize your App with a right-to-left language you can preview the layout by changing the Xcode scheme. From the scheme editor (⌘<) change the Application Language to “Right to Left Pseudolanguage” and launch the app. The interface will switch to right-to-left:

Trying that with our three labels gives us a user interface with the labels flipped to the right:

When You Do No Want Natural Alignment
There can be times when you want to override the natural direction. Suppose I have a label containing some cherry symbols with a green background that I want to fill the horizontal width of the view.

In this scenario I want my cherry label to be right-aligned for left-to-right layouts and left-aligned for right-to-left layouts. To see how to handle that let’s first force the label to be right-aligned for left-to-right layouts. Assume I have a property in my view controller for the cherry label:
var cherryLabel = UILabel()
The code to setup the label, right align it and add it to the superview:
cherryLabel.text = "
Supporting Right-to-Left Languages的更多相关文章
- Tips for thrift
Introduction I have designed and developed game servers successfully with thrift (http://thrift.apac ...
- Android官方文档翻译 十三 3.1Supporting Different Languages
Supporting Different Languages 支持不同语言 This class teaches you to 这节课教给你 Create Locale Directories and ...
- Android官方文档翻译 十二 3.Supporting Different Devices
Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...
- PLoP(Pattern Languages of Programs,程序设计的模式语言)
2014/8/1 12:24:21潘加宇 http://www.umlchina.com/News/Content/340.htmPloP大会2014即将举行 PLoP(Pattern Languag ...
- Natural language style method declaration and usages in programming languages
More descriptive way to declare and use a method in programming languages At present, in most progra ...
- ECSHOP \admin\edit_languages.php GETSHELL Based On Injection PHP Code Into /languages/zh_cn/user.php
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对于很多CMS网站来说,它们都需要保存很多的网站META信息,最常用的最佳实践是以 ...
- UVALive 6523 Languages
传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...
- Scripting Languages
Computer Science An Overview _J. Glenn Brookshear _11th Edition A subset of the imperative programmi ...
- BackgroundWorker Threads and Supporting Cancel
http://www.codeproject.com/Articles/20627/BackgroundWorker-Threads-and-Supporting-Cancel BackgroundW ...
随机推荐
- Jsp的语法和指令
Jsp的三种注释 前端语言注释:<!-- --> 会被转译,也会被发送,但是不会被浏览器执行 java语言注释: 会被转译,但是不会被servlet执行 Jsp注释:<%-- -- ...
- hdu-1698(线段树,区间修改)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 注意:用位运算会更快,不然超时. #include<iostream> #inclu ...
- Django基础(二)
https://www.cnblogs.com/yuanchenqi/articles/5716193.html
- Linux 随记
通配符和文件名变量:* ? [] * 查询 $ ls doc1 doc2 document mydoc monday $ ls doc* doc1 doc2 document $ ls *day m ...
- php PDO mysql
php PDO写法连接mysql: $db=new PDO("mysql:host=localhost;dbname=sql","root","roo ...
- C++和Python混合编程
为何人工智能(AI)首选Python?读完这篇文章你就知道了:https://blog.csdn.net/qq_41769259/article/details/79419322 C++调用Pytho ...
- (匹配)Fire Net --hdu --1045
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- GoF设计模式学习-单例模式
1.目的 控制实例的个数,类设计者应该保证只有一个实例,不能将此责任[只有一个实例]强制交给类使用者. 2.整体实现 1.单线程单例模式的实现. using System; using System. ...
- java web开发过程中的“\”指的是什么,如何区分
- linux系统编程之信号(二):信号处理流程(产生、注册、注销、执行)
对于一个完整的信号生命周期(从信号发送到相应的处理函数执行完毕)来说,可以分为三个阶段: 信号诞生 信号在进程中注册 信号在进程中的注销 信号处理函数执行 1 信号诞生 信号事件 ...