title: Get frame of a word in SwiftUI Text
body: <p>I want to find the position of a word in a sentence so that I can set a frame for that word.
  Like so for example: 
  <a href="https://i.stack.imgur.com/j05rm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j05rm.png" alt="enter image description here"></a></p>
  
  <p>Is it currently possible to do this in SwiftUI</p>
  
  <p>I wrote some code and this is what I got</p>
  
  <pre><code>import SwiftUI
  
  struct ContentView: View {
  
      @State var text = "I usually get ----- around nine o'clock every morning"
      @State var rects = [CGRect.zero]
  
      var body: some View {
          ZStack {
              TextView(text: $text, rects: $rects)
                  .overlay(
                      ForEach(0..&lt;self.rects.count, id: \.self) { index in
                          RoundedRectangle(cornerRadius: 6)
                              .frame(width: self.rects[index].size.width, height: self.rects[index].size.height)
                              .position(self.rects[index].origin)
                              .foregroundColor(Color.red)
                      }
              )
  
          }
      }
  }
  
  struct TextView: UIViewRepresentable {
  
      @Binding var text: String
      @Binding var rects: [CGRect]
  
      func makeCoordinator() -&gt; Coordinator {
          Coordinator(self)
      }
  
      func makeUIView(context: Context) -&gt; UITextView {
  
          let textView = UITextView()
          textView.delegate = context.coordinator
          textView.isEditable = true
          textView.isUserInteractionEnabled = true
          textView.font = UIFont.systemFont(ofSize: 24)
          DispatchQueue.main.async {
              let ranges = self.searchRanges(in: textView.text)
              self.rects = self.viewRects(for: ranges, textView: textView)
          }
          return textView
      }
  
      func updateUIView(_ uiView: UITextView, context: Context) {
          uiView.text = text
      }
  
      class Coordinator : NSObject, UITextViewDelegate {
  
          var parent: TextView
  
          init(_ uiTextView: TextView) {
              self.parent = uiTextView
          }
  
          func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -&gt; Bool {
              return true
          }
  
          func textViewDidChange(_ textView: UITextView) {
              self.parent.text = textView.text
          }
      }
  
      func searchRanges(in text: String) -&gt; [Range&lt;String.Index&gt;] {
          var ranges = [Range&lt;String.Index&gt;]()
          var searchRange = text.startIndex ..&lt; text.endIndex
          var range = text.range(
              of: "-----",
              options: .caseInsensitive,
              range: searchRange,
              locale: nil
          )
          while let findedRange = range {
              ranges.append(findedRange)
              searchRange = findedRange.upperBound ..&lt; text.endIndex
              range = text.range(
                  of: "-----",
                  options: .caseInsensitive,
                  range: searchRange,
                  locale: nil
              )
          }
  
          return ranges
      }
  
      func viewRects(for rnges: [Range&lt;String.Index&gt;], textView: UITextView) -&gt; [CGRect] {
          var rects = [CGRect]()
          for range in rnges {
              let upperBound = range.upperBound.utf16Offset(in: textView.text)
              let lowerBound = range.lowerBound.utf16Offset(in: textView.text)
              let length = upperBound - lowerBound
  
              if let start = textView.position(from: textView.beginningOfDocument, offset: lowerBound),
                  let end = textView.position(from: start, offset: length),
                  let txtRange = textView.textRange(from: start, to: end) {
                  var rect = textView.firstRect(for: txtRange)
                  rect.origin.x = rect.origin.x
                  rect.origin.y = rect.midY
                  rects.append(rect)
              }
          }
          return rects
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/r5EiY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r5EiY.png" alt="enter image description here"></a></p>
  
  <p><strong>Same code on UIKit with storyboard</strong></p>
  
  <p><a href="https://i.stack.imgur.com/QcPe8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QcPe8.png" alt="enter image description here"></a></p>