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) not NSTextAlignmentLeft (.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的更多相关文章

  1. Tips for thrift

    Introduction I have designed and developed game servers successfully with thrift (http://thrift.apac ...

  2. Android官方文档翻译 十三 3.1Supporting Different Languages

    Supporting Different Languages 支持不同语言 This class teaches you to 这节课教给你 Create Locale Directories and ...

  3. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  4. PLoP(Pattern Languages of Programs,程序设计的模式语言)

    2014/8/1 12:24:21潘加宇 http://www.umlchina.com/News/Content/340.htmPloP大会2014即将举行 PLoP(Pattern Languag ...

  5. 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 ...

  6. ECSHOP \admin\edit_languages.php GETSHELL Based On Injection PHP Code Into /languages/zh_cn/user.php

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对于很多CMS网站来说,它们都需要保存很多的网站META信息,最常用的最佳实践是以 ...

  7. UVALive 6523 Languages

    传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...

  8. Scripting Languages

    Computer Science An Overview _J. Glenn Brookshear _11th Edition A subset of the imperative programmi ...

  9. BackgroundWorker Threads and Supporting Cancel

    http://www.codeproject.com/Articles/20627/BackgroundWorker-Threads-and-Supporting-Cancel BackgroundW ...

随机推荐

  1. 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)

    Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...

  2. 2018.08.29 NOIP模拟 pmatrix(线性筛)

    [问题描述] 根据哥德巴赫猜想(每个不小于 6 的偶数都可以表示为两个奇素数之和),定义 哥德巴赫矩阵 A 如下:对于正整数对(i,j),若 i+j 为偶数且 i,j 均为奇素数,则 Ai,j = 1 ...

  3. 2018.07.23[PA2015]Siano(线段树)

    [PA2015]Siano 描述 Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘 ...

  4. python小练习--函数调用函数,让对象具有能动性

    class Box:#定义一个类名为Box,类名后不必有括号,类包含类属性和类方法,这个类没有定义类属性 '''这是一个计算体积的类'''#这是这个类的__doc__属性,执行类后就可以在交互界面输入 ...

  5. GitHub 安装配置

    1:到 Github 注册 页面中注册,填写用户名.邮箱和密码 选择免费服务 步骤三可以根据自身喜好勾选或者直接跳过 2.1.2 创建远程仓库 创建完账号后,可以开始创建仓库 但是这里我们还没有验证邮 ...

  6. HDU 3681 Prison Break (二分 + bfs + TSP)

    题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...

  7. authentication 和 authorization

    单词 词性 解释 authentication n. 认证 authentic adj. 真实的 authorization n. 授权 authorise vt. 授权 authentication ...

  8. delphi 升级到xe7后的一些个人经验

    http://blog.csdn.net/span12/article/details/42522091 你只要记住 字符串使用变了.VCL 下面所有的 char 改 ansichar string ...

  9. Spring框架事务支持模型的优势

    全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...

  10. Ajax 访问 或 获取 IIS 虚拟目录

    使用场景 最近用 .net core mvc 写了一个工具类的项目,作为我们项目的后台管理网站使用.第一次被老大拿去部署的时候被告知不可用,同样的代码在我电脑和我的iis上都可以使用的啊. 后来才知道 ...