title: How to get offset from start of line of UITextRange.start
body: <p>I have an UITextView which can have multiple lines. All I'm interested in is the position of the cursor on a certain line (where it doesn't matter whether first, second, third line etc.).</p>
  
  <p>I've been logging UITextRange.start which gives me exactly what I need, namely the offset property:</p>
  
  <pre><code>cp &lt;UITextPositionImpl: 0x6e93260&gt; &lt;&lt;WebVisiblePosition: 0x6e92d00&gt;(offset=5, context=([d|], [u+0064|u+0000])&gt;
  </code></pre>
  
  <p>My question is how I get the offset=5 into a simply integer?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>UITextRange *caretPositionRange = textView.selectedTextRange;
  NSLog(@"cp %@", caretPositionRange.start);
  </code></pre>
  
  <p>All I need would be something like int cp = caretPositionRange.start.offset which doesn't work.</p>
  
  <hr>
  
  <p>edit:</p>
  
  <p>to clarify, I'm particularly interested in the cursor position of each line, not the entire TextView. So this won't really work:</p>
  
  <pre><code>UITextRange *caretPositionRange = tv.selectedTextRange;
      int caretPosition = [tv offsetFromPosition:tv.beginningOfDocument
                              toPosition:caretPositionRange.start];
  </code></pre>
  
  <p>as this would give me a different position for each line.</p>
  
  <hr>
  
  <p>edit 2:</p>
  
  <p>the answer below given by Jesse works really well. First time around, I got an EXC_BAD_EXCESS as I didn't check if startOfLine = nil, so keep in mind checking if it's not nil.</p>