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. 做SEO都需要具备哪些方面的知识

    做seo需要了解的基本知识有利于seo工作的进行 一.了解搜索引擎的工作原理 搜索引擎的基本工作原理包括如下三个过程: 1.首先在互联网中发现.搜集网页信息; 2.同时对信息进行提取和组织建立索引库; ...

  2. 2018.07.13 [HNOI2015]落忆枫音(容斥原理+dp)

    洛谷的传送门 bzoj的传送门 题意简述:在DAG中增加一条有向边,然后询问新图中一共 有多少个不同的子图为"树形图". 解法:容斥原理+dp,先考虑没有环的情况,经过尝试不难发现 ...

  3. jdk10运行springboot项目出现:Type javax.xml.bind.JAXBContext not present

    项目由openjdk8.0迁移到jdk10导致的 原因:java9模块化的概念使得JAXB默认没有加载: jaxb-api是存在jdk中的,只是默认没有加载而已,手动引入即可. 推荐方式: <! ...

  4. IntelliJ IDEA 2017版 spring-boot使用JdbcTemplate实例

    搭建总框架: (1)在pom.xml加入jdbcTemplate的依赖: (2)编写Dao类,声明为:@Repository,引入JdbcTemplate (3)编写Service类,引入Dao进行使 ...

  5. 对MVC模型的自悟,详尽解释,为了更多非计算机人员可以理解

    今天小编在复习之前刚刚学会的一个小项目,然后突然对MVC有了新的理解,决定迅速将其写成文档,否则可能会忘记,就算是一个顿悟,学了java语言好久了,刚刚才对其有了比较深入的理解,希望对于同样的人能够有 ...

  6. 深入浅析JavaScript中with语句的理解

    JavaScript 有个 with 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象. with语句的作用是暂时改变 ...

  7. struts2 的特征

    web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache ...

  8. 三)mybatis 二级缓存,整合ehcache

    mybatis-config.xml <setting name="cacheEnabled" value="true" /> PersonMapp ...

  9. Android属性动画之ValueAnimator的介绍

    之前两篇博客,介绍的是ObjectAnimator作用与某一个控件的某一个属性.但我们的ValueAnimator它本身并不会作用与任何一个属性,它本身也不会提供任何一种动画.它简单的来说,就是一个数 ...

  10. (最短路 dijkstra)昂贵的聘礼 -- poj -- 1062

    链接: http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...