-1

Given an NSRange from a UITextView in Swift, how can I retrieve the center coordinates of the rectangle that would wrap it? All the answers I could find here are for objective-c.

Doruk Karınca
  • 195
  • 1
  • 12
  • It would be the same APIs in either language. Make an attempt to translate the Objective-C code. Post your attempted Swift code and, if needed, the corresponding Objective-C code. Explain what problems you are having with the translation. – rmaddy Sep 18 '18 at 23:27

1 Answers1

1

As per Pushp's answer:

let range: NSRange = (txtView.text as NSString).range(of: text)
let beginning: UITextPosition? = txtView.beginningOfDocument
let start: UITextPosition? = txtView.position(from: beginning!, offset: range.location)
let end: UITextPosition? = txtView.position(from: start!, offset: range.length)
let textRange: UITextRange? = txtView.textRange(from: start!, to: end!)
let rect: CGRect = txtView.firstRect(for: textRange!)
Doruk Karınca
  • 195
  • 1
  • 12
  • 1
    Normally, Instead of reposting someone else's answer, you close the question as a duplicate. Since it was your own question you should just delete your question since it has already been answered. – rmaddy Sep 18 '18 at 23:57
  • @rmaddy The so-called duplicate is a question tagged with objective-c and its marked-as-correct answer is in objective c. I copied and attributed the answer for those who need an answer in swift. The original answer was under the wrong question and therefore was difficult to find; do you want those having the same problem waste time looking for an answer where it isn't meant to be? – Doruk Karınca Sep 24 '18 at 21:02