I have an Arabic (right to left) text in textView, when I set myTextView.alignment = .justify
, the last line in text go to the left side. How can I set center justify for textView or how can move last line to the center?

- 30,560
- 17
- 97
- 143

- 103
- 7
3 Answers
just make it .center i know what exactly you're looking for cause i've been there before, only you can make it .center

- 765
- 1
- 7
- 17
-
can't use center and justify together?? – Mostafa Habibi Aug 06 '17 at 06:53
-
2no you can't, otherwise developers would be allowed to use both .right and .left – Sameh Salama Aug 06 '17 at 07:28
Interesting question, I would try to count the lines in the textView and cut the last one than I would try to put the cut text in a new label below the textView ... ;)
after asking how I would do this I added this:
print(self.testLabel.frame.width)
self.testLabel.text = "Hello World"
self.testLabel.sizeToFit()
print(self.testLabel.frame.width)
This is a starting point you can work with. Create a label and set the width to zero. Insert your text and set sizeToFit(). Now you are able to get the length of the label. Is it to long for your view it will have more lines... (The divider is your length - not tested, maybe an offset is needed too)
The idea is now to subtract as long as your lines are more than one and the lines are the same amount at the beginning of your calculation to get the last word and put it at the beginning of a temp string. If this returns you get your substring for the last line.
I was interested and started with this one:
@IBOutlet weak var textBox: UITextView!
@IBOutlet weak var testLabel: UILabel!
override func viewWillAppear(_ animated: Bool) {
print(self.testLabel.frame.width)
self.testLabel.text = self.textBox.text
self.testLabel.sizeToFit()
print("total \(self.testLabel.frame.width)")
let basicAmount = Int(self.testLabel.frame.width / self.textBox.frame.width) + 1
print("lines \(basicAmount)")
var lastLine: String = ""
while basicAmount == Int(self.testLabel.frame.width / self.textBox.frame.width) + 1 {
var arr = self.textBox.text.components(separatedBy: " ")
lastLine = "\(arr.last) \(lastLine)"
arr.removeLast()
self.textBox.text = arr.joined(separator: " ")
self.testLabel.text = self.textBox.text
self.testLabel.sizeToFit()
print(lastLine)
}
}
the interesting output is:
total 1499.5
lines 7
Optional("civiuda.")
Of course you should spent more time in the calculation because of the free space at the end of a line...

- 891
- 1
- 11
- 18
-
use this to count the lines (at the end is swift3): https://stackoverflow.com/questions/5837348/counting-the-number-of-lines-in-a-uitextview-lines-wrapped-by-frame-size – Johannes Knust Aug 06 '17 at 14:02
-
-
you’re wrong.. lastLine = "\(arr.last) \(lastLine)" extends the string. (add a new word at the beginning) – Johannes Knust Aug 09 '17 at 07:45
use this
self.textView.textAlignment = .justified //set text alignment center and justified
self.textView.makeTextWritingDirectionRightToLeft(true)//if you want set writing direction right to left
self.textView.makeTextWritingDirectionLeftToRight(true)//if you want set writing direction left to right

- 455
- 9
- 19