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 6126581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:23:36+00:00 2026-05-23T16:23:36+00:00

How can I tell UITextView to set the cursor to a particular line (line

  • 0

How can I tell UITextView to set the cursor to a particular line (line 13, for example) when it appears?

  • 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-05-23T16:23:36+00:00Added an answer on May 23, 2026 at 4:23 pm

    UITextView has a method setSelectedRange: (NSRange) range.

    If you know where in the string line 13 occurs, say location 237, then do: [textView setSelectedRange: NSMakeRange(237,0)];

    If you need to find out where line 13 occurs, you’ve more work ahead. I’d start by looking at sizeWithFont, remembering to nip about 16 points off the width of your textView so that iOS gets the sums right. (That said, if you have line breaks, then just find the location of the 13th (or nth) “\n”.)

    Update following your further query in the comment

    There are lots of ways of finding the position of the nth \n. The following snippet isn’t pretty, but it will do the job. You could also use rangeOfString and iterate through the “\n”. In this snippet, if the target line is greater than the number of lines, it puts the cursor at the end. The code here assumes you have a UITextView property called userEntry.

    int targetLine = 3; // change this as appropriate 0=first line
    
    NSRange range;
    
    NSString* exampleString = @"Hello there\nHow is it going?\nAre you looking for a new line?\nA new line in what?\nThat remains to be seen";
    
    NSArray* separateLines = [exampleString componentsSeparatedByString:@"\n"];
    
    if (targetLine < [separateLines count])
    {
        int count = 0;
        for (int i=0; i<targetLine; i++)
        {
            count = count + [[separateLines objectAtIndex:i] length] + 1; // add 1 to compensate \n separator
        }
    
        range = NSMakeRange(count, 0);
    }
    else
    {
        range = NSMakeRange([exampleString length], 0); // set to the very end if targetLine is > number of lines
    }
    
    [[self userEntry] setText: exampleString];
    [[self userEntry] setSelectedRange:range];
    [[self userEntry] becomeFirstResponder];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somebody can tell me an example of using locking mechanism based on futex? (for
How can tell if my object's value is a float or int? For example,
can please anyone can tell me to how set the row colors of the
Anyone can tell me which one is better (JAXB or Apache XMLBeans) taking in
Anyone can tell me what I'm doing wrong? I am creating a simple system
Which API can tell me the information about if LED is supported by a
so from what i can tell, you have to specify artifacts and working directory
As far as I can tell i'm doing everything by the book, but the
As far as I can tell, there is no API (official or unofficial) to
Any idea how I can tell AutoMapper to resolve a TypeConverter constructor argument using

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.