Does autolayouts supports RTL views using constraints? - ios

I have localized my application in arabic. While in arabic culture, I need to mirror my views using constraints like trailing and leading. what are the best practices to have a flipped view?
It may be interesting to note that when set to Arabic, its culture is set as #"ar-AE". I guess flipping the view doesn't work with the #"ar-AE" locale, although I'm unsure whether that's a bug, or if some arabic countries write LTR, thus making sense not to systematically flip the view.
Long story short what constraint settings are required to actually convert view into "Arabic" and properly flip a view?

Yes, using Leading and Trailing constraints is in theory all you should have to do.
The language variant should be independent of the appended region, and fall back to the generic language for layout information. If you are seeing LTR views in ar but not ar-AE, you should file a bug at bugreport.apple.com.

Related

localizing ios application with longer strings

I need to localize my iOS application, from EN (English) to FR (Francais). the problem - most of FR strings are much longer than EN strings, so if I use the same UI, it does not look well with FR strings
When localizing android application, I used different resource files for each language, with smaller font, larger height, etc. ...
What is equivalent iOS solution for longer strings in different language?
for changing the font size, I can use this solution: - Swift3 different font in the All of the UIView with Localization each
Is there a simpler solution, in which I can see at design time how the screen is displayed?
You can fit texts into your views for all length using Auto Layout regardless of Font, Language, Orientation, DeviceSize etc.
There is a sample blog post for some tips for that:
Tips for using Auto Layout when localizing apps
Remove all fixed-width constraints. If the German text is 30% longer,
and you don’t provide room for it in your UI, this will at least let
iOS change the font size to accommodate. Otherwise, your localized
text will get cropped.
Text fields should fit to contents. Select Editor > Size To Fit
Content so that text fields and labels resize automatically for
longer or shorter text.
Pin views to adjacent views. This way, when one view resizes to fit
your localized text, the other views will too. Otherwise, views may
overlap in some languages
No minimums or maximums. Allow each content view to adjust in size as the language changes.
Use leading and trailing attributes instead of left and right. This
tip will make right-to-left languages (Arabic, Hebrew) flow properly.
You can also have look at Introduction to Auto Layout
You can use the following code snippet to determine the language setting of a device:
Locale.current.languageCode
And than you can change/set the font size accordingly

How can I force my app to use the inverse text direction for my Auto Layout constraints?

I've designed my app to use Leading / Trailing constraints.
I do not have localizations for R2L languages but need to show an overlay on windows that might use that direction, and then I need to swap the direction of my controls to match those of the targeted app.
I can figure out which direction the other application uses.
What I can't figure out is an easy way to have my own window use the R2L text direction even though it wants to use L2R direction by default.
I could manually replace all my constraints at runtime, by using Left / Right instead of Leading / Trailing constraints, and then swap them out with the opposite constraints if required. But before I make that effort I like to know if there's an easier way by I can just tell the Auto Layout system to use the opposite direction.
You can override -userInterfaceLayoutDirectionForSemanticContentAttribute: (in Swift, userInterfaceLayoutDirection(for:)) to return the desired layout direction for your view.

iOS Localizations mess up the UI

I have an iOS Application that supports different languages, which uses LTR Storyboard and RTL Storyboard, and reads from Plists. When I run the app on my Device with English iOS Version, everything looks perfect when switching between the languages. But when I change the iPhone OS Language to RTL languages. The UI is messed up.
This happens especially when I enable Localizations to change the app name on home screen.
Any idea why does that happen?
The whole menu is messed up for example, it's to the left side while it should be to the right side, and the menu items icons must be to the left and the labels to the right.
RTL Language on LTR iOS Language Device i.e English
RTL Language on RTL iOS Language Device
This is because you are use leading and trailing constraint.
In a LTR environment leading means left while in an RTL environment, leading means right.
This is Apple way of helpings us to deal with RTL/LTR easily :[ in your case, not so much...
To disable this behaviour, change the semantic attribute of your views.
Semantic Content
If you lay out your views using leading and trailing constraints, the
views automatically flip positions when switching between
left-to-right languages (like English) and right-to-left languages
(like Arabic). However, some interface elements should not change
their position based on the reading direction. For example, buttons
that are based on physical directions (up, down, left, and right)
should always stay in the same relative orientation.
The view’s semanticContentAttribute property determines whether the
view’s content should flip when switching between left-to-right and
right-to-left languages.
In interface builder, set the Semantic option in the Attribute
inspector. If the value is Unspecified, the view’s content flips with
the reading direction. If it is set to Spatial, Playback, or Force
Left-to-Right, the content is always laid out with the leading edges
to the left and trailing edges to the right. Force Right-to-Left
always lays out the content with the leading edges to the right and
the trailing edges to the left.
Assuming that you are already using Auto Layout,
Make sure you have done the following
Remove fixed width constraints. Allow views that display localized text to resize. If you use fixed width constraints, localized text may appear cropped in some languages.
Use intrinsic content sizes. The default behavior for text fields and labels is to resize automatically. If a view is not adjusting to the size of localized text, select the view and choose Editor > Size To Fit Content.
Use leading and trailing attributes. When adding constraints, use the attributes leading and trailing for horizontal constraints. For left-to-right languages, such as English, the attributes leading and trailing are equivalent to left and right. For right-to-left language, such as Hebrew or Arabic, leading and trailing are equivalent to right and left. The leading and trailing attributes are the default values for horizontal constraints.
Pin views to adjacent views. Pinning causes a domino effect. When one view resizes to fit localized text, other views do the same. Otherwise, views may overlap in some languages.
Constantly test your layout changes. Test your app using different language settings, as described in Testing Your Internationalized App.
Don’t set the minimum size or maximum size of a window. Let the window and its content view adjust to the size of the containing views, which may change when the language changes.
See more
Probably a matters of constraints. In the storyboard, you can use the preview editor to get a .. well.. preview, of your UI in diffrent device size in different language.

Auto RTL, LTR constraints behavior

Leading and Trailing constrains behaives depending on device langauge direction. To create an app that only support english requires to select every trailing/leading constraint and disable the respect language direction features. that converts the constraints to left/right constraints.
I remember that I used to disable creating leading/trailing constraints by default. instead, my constraints created as left/right constrains by default.
I am not quite sure how I used to do that. I thought that its related to the Use Base Internationalization feature in the project general settings. That didn't work.
I remember before that the project by default disables the Use Base Internationalization feature in older xcode versions. now its enabled by default.
Any idea how I can automatically force left/right constraints instead of leading/trailing constraints?
Edit:
I just run an app that use base internationalzation and the constraint is leading/trailing and it didnt go RTL even that the device language is Arabic!
As long as you only support English as a localization in your project, there should be no work needed to guarantee that your app will always be LTR, even with leading/trailing constraints.

Auto Layout inverts views horizontally iOS

I haven’t been able to pinpoint the cause of an issue i’m having , regarding our company project.
I have notice that in all the auto layout views in the project , the views that appear to be attached to a specific side in interface builder,
are presented on the opposite side after the build.
I.e when i am adding a view to a storyboard view controller, than creating an horizontal space constraint to it’s superview leading margin (left side) it appears ok on interface builder and the preview, but after the build, it appears to be inverted on simulator/device , the view has the same horizontal space but to the opposite side ( right side on this example).
The same issue happens to all the views that are already were on the app (the views that supposed to be on the right side appear to be on the left side and vice versa )
I spend hours to figure out what cause this invert with out any success (the issue happens only in our project), i even tried to change the project definitions according to a new one.
I’ll appreciate if you will be able to help me on this matter.
I'am adding the screenshot of the view in interface builder, the preview and the simulator
There are two kinds of horizontal constraint — universal and language-bound.
The universal constraints are called Left and Right. The meanings of these constraints never change.
The language-bound constraints are called Leading and Trailing. Their meaning depends on the language environment in which the app is running. If the language is like English, Leading is left and Trailing is right; but if the language is like Arabic or Hebrew, which are written from right to left, Leading is right and Trailing is left. This allows labels to be constructed that work correctly regardless of writing direction.
This distinction is drawn in Interface Builder with the "Respect language direction" menu item; to get the universal Left-Right constraints, uncheck it:
So, you may be using Leading and Trailing correctly and appropriately for some views, but it sounds from your complaint as if you have accidentally used Leading and Trailing for some views where you meant to use Left and Right.
It happened when you have Right-to-left system language, for example hebrew or arabic. It's default Apple feature.
If you change system language to English everything should be ok.

Resources