0

i am trying text enter in textview right to left alignment but at that time textview starting position enter space not taking textview in swift

@IBOutlet weak var textview: UITextView! @IBOutlet weak var sampleTF: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    textview.textAlignment = .right
    textview.isScrollEnabled = true
    textview.heightAnchor.constraint(equalToConstant: 52.0).isActive = true
    textview.delegate = self
    
}

@IBAction func sampleButton(_ sender: Any) {
 
    
}

func textViewDidChange(_ textView: UITextView) {
    let size = CGSize(width: textView.frame.width, height: 200)
    let estimateSize = textView.sizeThatFits(size)
    
    guard textView.contentSize.height < 100.0 else { textview.isScrollEnabled = true; return}
    
    textview.isScrollEnabled = false
    textview.constraints.forEach { (constriant) in
        if constriant.firstAttribute == .height {
            constriant.constant = estimateSize.height
        }
    }
}
Manoj
  • 1
  • 1
  • can you provide some code of what you have already. just to give us a bit of context – colin Apr 07 '21 at 12:07
  • don't share your personal email address in a question or thread. Your question should be answered in this thread and nowhere else. – colin Apr 07 '21 at 12:16
  • I'm struggling to understand what the problem is. I'm not sure I understand the question, and the code supplied doesn't seem to be related to whatever the question is. – flanker Apr 07 '21 at 12:41
  • Does the following link answer your question ? https://stackoverflow.com/questions/13441940/is-there-a-bulit-in-method-to-enter-text-from-right-to-left-in-an-ios-6-uitextfi – Ptit Xav Apr 07 '21 at 12:56
  • textview right alignment starting position space not show – Manoj Apr 07 '21 at 12:56

1 Answers1

0

As seen on other posts it has to do with how spaces are handled (). You can add the following delegate code to replace spaces with non breaking spaces :

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -> Bool {
    if (textView == self.textView) {
        let oldString = textView.text!
        let newStart = oldString.index(oldString.startIndex, offsetBy: range.location)
        let newEnd = oldString.index(oldString.startIndex, offsetBy: range.location + range.length)
        let newString = oldString.replacingCharacters(in: newStart..<newEnd, with: string)
        textView.text = newString.replacingOccurrences(of: " ", with: "\u{00a0}")
        return false;
    } else {
        return true;
    }
}
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15