Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:51:07+00:00 2026-06-05T07:51:07+00:00

I have an UITextView which can have multiple lines. All I’m interested in is

  • 0

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.).

I’ve been logging UITextRange.start which gives me exactly what I need, namely the offset property:

cp <UITextPositionImpl: 0x6e93260> <<WebVisiblePosition: 0x6e92d00>(offset=5, context=([d|], [u+0064|u+0000])>

My question is how I get the offset=5 into a simply integer?

Here is my code:

UITextRange *caretPositionRange = textView.selectedTextRange;
NSLog(@"cp %@", caretPositionRange.start);

All I need would be something like int cp = caretPositionRange.start.offset which doesn’t work.


edit:

to clarify, I’m particularly interested in the cursor position of each line, not the entire TextView. So this won’t really work:

UITextRange *caretPositionRange = tv.selectedTextRange;
    int caretPosition = [tv offsetFromPosition:tv.beginningOfDocument
                            toPosition:caretPositionRange.start];

as this would give me a different position for each line.


edit 2:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:51:09+00:00Added an answer on June 5, 2026 at 7:51 am

    You’ll need to ask the UITextView via its UITextInput methods. Something like this:

    UITextPosition *pos = caretPositionRange.start;
    id<UITextInputTokenizer> tokenizer = [textView tokenizer];
    UITextPosition *startOfLine = [tokenizer positionFromPosition:pos 
                                                       toBoundary:UITextGranularityLine
                                                     inDirection:UITextStorageDirectionBackward];
    if (startOfLine != nil) {
        // based on some experiments, startOfLine may be nil for, eg, empty text views
        // the next line crashes if you pass nil, so we check first
        NSUInteger offset = [textView offsetFromPosition:startOfLine
                                              toPosition:pos];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view with multiple UITextView s which need to be tappable to
I have UIViewController which contain UIImageView and UITextView . All views (and their frames)
I have a UITextView in which i have 5 hyperlinks and 5 phone numbers.How
I have HTML Content which was being displayed in a UITextView . The next
I have a XIB file with a UITextView, which I'm associating with a UITextView*
I'm writing iPhone/iPad application. I have UITextView in which I automatically appending text. How
I have a keyboard which edits text in a UITextView and tapping the return
I have a view with two UITextView elements in it, each of which is
I have UITextView, which is left aligned. When last word does not fit on
In a UITextView have a text which is for readonly and dont want any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.