Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why (arabic with english) text refuses to align right?

This is the string I use:

CASE 1

    var word1 = "عبد الله"
    var word2 = "restaurant"
    label.text = " \(word1) found your review on \(word2) useful."

Result:

a

CASE 2

    var word1 = "عبد الله"
    var word2 = "restaurant"
    label.text = "note: \(word1) found your review on \(word2) useful."

Result:

b

Question

so, how do I make the first word to wrap right? if the first word is arabic, it gets wrapped to the left, but if the first word is english the situation is expected, so how make the word1 to show up when first word on the left?

I tried both

label.textAlignment = NSTextAlignment.Left

and

label.textAlignment = NSTextAlignment.Natural

without any luck.

like image 947
DeyaEldeen Avatar asked Oct 13 '16 13:10

DeyaEldeen


People also ask

Is Arabic text right aligned?

Arabic language is a right to left language (RTL) which means writing starts from the right and continues to the left. However, when text is right-justified explicitly, it should have the same alignment as English.

How do I make Arabic English on the same line in Word?

We need to enable the RTL for writing arabic and english together. Start typing Arabic by changing the keyboard then again English by changing the keyboard layout. You will have to enable the RTL throught the document.

Is Arabic left aligned?

Arabic script is written from right to left.

Why is my Arabic text not showing up in Microsoft Word?

If the chosen font doesn’t have the required Arabic character set, the program may display the text as blank boxes, pink squares or similar, or gibberish text. In Microsoft Word though, generally you can’t change Arabic text to a font that can’t display it.

Why can't I use the Arabic translation with my client?

However problems can arise if clients then make changes to those files, or attempt to use the Arabic translation elsewhere. The text display can change, and be incorrect. This can happen even when making minor formatting adjustments, or simply copying and pasting text to another application.

Why are my Arabic text files jumbled up in word?

(2) Some Texts in Arabic on the net are created using the Arabic Software which when pasted in an English language version of Word get jumbled up because Arabic runs from Right-to-Left whilst English runs from Left-to-Right.

How to display Arabic text right to left (RTL)?

With the below code we can display english (latin) text left to right (LTR) and arabic (hebrew) text right to left (RTL) with out much complexity. lv_cnt type i. lv_name = ‘فارون’. “Arabic text lv_cnt = strlen( lv_name ). “Finding the length of the arabic text.


2 Answers

Unicode has two marker characters (LTR: 0x200E, RTL:200F). These are invisible, but control the direction, I just need to add this \u{200E} to force the wrapping direction.

 \u{200E} \(word1) found your review on \(word2) useful.

EDIT: see full tutorial here, for more info.

like image 176
DeyaEldeen Avatar answered Nov 14 '22 21:11

DeyaEldeen


UILabel as a subclass of UIView has a variable named semanticContentAttribute which you can set to .foreRightToLeft, it can also be set from the nib inspector through the Semantic pop-up menu in the attributes inspector.

Moreover, you can query effectiveUserInterfaceLayoutDirection property for debugging it's state.

See this for reference.

Now if you need both alignments in one label it will be tricky, either group two labels in a container UIView or see if you can set these values for portions of an NSMutableAttributedString which you can feed to a UILabel.

The textAlignment properties you are trying to set will give you the same effect that MS-Word does to paragraph alignment but wouldn't flip reading direction for language.

Happy coding!

Edit: This is an example of what I am suggesting with attributed strings although when changing the arabic setting to RightToLeft it puts it at the bottom of the string... Maybe the flags need to be combined differently?

override func viewDidLoad() {
    super.viewDidLoad()
    let label = UILabel()

    let myMutableString = NSMutableAttributedString()

    //right-to-left
    let multipleAttributes: [String : AnyObject] = [
        NSForegroundColorAttributeName: UIColor.orangeColor(),
        NSBackgroundColorAttributeName: UIColor.blueColor(),
        NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
        NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue ]
    ]

    let myAttrString = NSAttributedString(string: "عبد الله", attributes: multipleAttributes)

    myMutableString.appendAttributedString(myAttrString)

    //some-text

    let someText = NSAttributedString(string: " finds ", attributes: nil)

    myMutableString.appendAttributedString(someText)

    //left-to-right
    let multipleAttributes2: [String : AnyObject] = [
        NSForegroundColorAttributeName: UIColor.blueColor(),
        NSBackgroundColorAttributeName: UIColor.yellowColor(),
        NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,
        NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue | NSTextWritingDirection.Embedding.rawValue]
    ]

    let myAttrString2 = NSAttributedString(string: "restaurant", attributes: multipleAttributes2)

    myMutableString.appendAttributedString(myAttrString2)

    label.attributedText = myMutableString

    self.view.addSubview(label)
    label.sizeToFit()
    label.center = self.view.center

}
like image 34
jugutier Avatar answered Nov 14 '22 21:11

jugutier