This is the string I use:
var word1 = "عبد الله"
var word2 = "restaurant"
label.text = " \(word1) found your review on \(word2) useful."
Result:
var word1 = "عبد الله"
var word2 = "restaurant"
label.text = "note: \(word1) found your review on \(word2) useful."
Result:
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.
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.
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.
Arabic script is written from right to left.
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.
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.
(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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With