0

I am using a dynamic textview.

func textViewDidChange(_ textView: UITextView) {
        let str = textView.text! as NSString
        let size = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20.0)])
        textView.frame = CGRect(origin: textView.frame.origin, size: size)
    }

This is the code used for dynamic change in height and width. This works well while using "English". The problem is when i change language to "arabic", text view has to increase width towards the left, but it still increasing the width towards the right.

Faheem Rahman
  • 343
  • 2
  • 10
  • iPhone screen co-ordinate system is like origin is at the top left corner, so when u move towards right side X co-ordinate increases, and when you move towards down Y co-ordinate increases. So it will always be from left to right on width increase. So to achieve what you want start your textView origin from right most and keep decreasing the X co-ordinate and set the size, then it will work. – vivekDas Jul 16 '18 at 11:53
  • i didnt get you.. sorry – Faheem Rahman Jul 16 '18 at 11:56

1 Answers1

0

First check the iPhone screen co-ordinate system below. enter image description here

I am saying if you increase the width it will always increasing the width towards the right in iPhone. So set your initial origin of the UITextView at the right most and then decrease origin's X Co-ordinate by the amount 'size' you calculate and then set the new origin and size values to the UITextView frame. Hope u understand.

For example :

lets say your initial UITextView origin is at (300,30) and size is (30,30) now you enter some text Arabic text, and you calculate the size of the text as 100. Now your new origin's X will be 300 - 100 = 200

so newOrigin = (200,30)
and newSize = (100,30)

now set the UITextView frame as- textView.frame = CGRect(origin: newOrigin, size: newSize)

vivekDas
  • 1,248
  • 8
  • 12