Mbf
Mbf

Reputation: 13

Write RTL String on a UILabel

I receive two Strings from a server

firstString = "JHKJ"
secondSctring = "ذيس إز اى تست" 

On LTR mode, I want the label to print JHKJ: ذيس إز اى تست and on RTL I expect it to print ذيس إز اى تست : JHKJ

If I normally concatenate the strings

let finalString = firstString  + ":" + secondSctring

The label shows the same result on both RTL and LTR:

JHKJ:  ذيس إز اى تست

And even if I change the concatenation order

if rtl{
finalString =  secondSctring + ":" + firstString
}else{
finalString = firstString  + ":" + secondSctring
}

myLabelView.text = finalString

the result still the same :

JHKJ:  ذيس إز اى تست 

I also tried to force th label to use only LTR mode, it didn't helped. I also tried to use two attributed string and append them in a third one, it also didn't work. Do you have an idea on how to solve this without using two labels?

Upvotes: 1

Views: 939

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16456

Before some time I faced same issue and I managed to do it with

The 0x200E unicode character is invisible but puts the rendering back into left-to-right mode.

After the above, this is the output that I'm getting:

let firstString = "JHKJ"
let secondSctring = "ذيس إز اى تست"

let finalString = firstString  + ":" + secondSctring

var result1: String = "\u{200E}\(secondSctring) : \(firstString)"
var result2: String = "\u{200E}\(firstString) : \(secondSctring)"

result1 ذيس إز اى تست : JHKJ

result2 JHKJ : ذيس إز اى تست

Hope it is helpful to you

Upvotes: 3

Related Questions