0

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?

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Lib3Rian
  • 21
  • 9

2 Answers2

3
  • 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.
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Actually there is one whole text which looks like as 10 lines on simulator and when I'm trying to get components(separatedBy: .newlines) it only gets one whole text. – Lib3Rian Jun 24 '20 at 07:26
  • If there are no *physical* lines how do you know what the 5th line is? There is no way unless you know the partial string where the line break is supposed to be inserted. – vadian Jun 24 '20 at 07:30
  • Let's assume that 5th line is 5th line shown on simulator, is there a way to insert line break on that line without knowing partial string? – Lib3Rian Jun 24 '20 at 07:50
  • 1
    *On simulator* what? iPhone SE, 11 max, iPad something? No, you need to be able to identify the insertion location independent of the UI. – vadian Jun 24 '20 at 08:32
0

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

Ajjjjjjjj
  • 669
  • 4
  • 12