Lib3Rian
Lib3Rian

Reputation: 21

How to insert line break at the end of specific line of UITextView

I have UITextView with 10 lines and now I'm trying to insert line break at the end of 5th line. How is it possible?

Upvotes: 0

Views: 348

Answers (2)

Ajjjjjjjj
Ajjjjjjjj

Reputation: 675

here is :

 let textview = UITextView()
        textview.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
        textview.text = "djhgiruior seitii iuuiuy  yyt  yt tr dtre d grf hth yghj  jh u hu khku yi  hhhhh\nuiytuftftyftyfytf"
        textview.backgroundColor = .green
        self.view.addSubview(textview)
        

It will give new line after '\n' . So use this

Upvotes: 0

vadian
vadian

Reputation: 285069

  • Split the text into lines with components(separatedBy: .newlines)
  • Get the 5th line (at index 4!)
  • Get the range of the line in the text with range(of:)
  • Insert the line break at the upperBound index of the range.

Upvotes: 3

Related Questions