Developers Corner



There doesn't appear to be a specific method to find the range of a specific line in the current foundation (OS4.2). However, the following category method of NSString should accomplish the desired results. Note, this has only been tested on OS4.2 for Mach.
#define INRANGE(a,b) (a.location + a.length < b)
- (NSRange) rangeOfLine: (unsigned int) lineNumber
  NSRange lineRange;
  int lineCount, myLength;

  myLength = [self length];
  lineCount = 0;

  lineRange.location = 0;
  lineRange.length = 0;

  // find line
  lineRange = [self lineRangeForRange: lineRange];
  while((lineCount < lineNumber) && INRANGE(lineRange,myLength)){
    lineRange.location += lineRange.length;
    lineRange.length = 0;
    lineRange = [self lineRangeForRange: lineRange];

  // if still in range
    return lineRange;

  lineRange.location = NSNotFound;
  lineRange.length = 0;
  return lineRange;

As for finding the ranges of the lines intersecting a given line, run the example and select some text in the top scroll view. The ranges of the lines you are intersecting will appear in the bottom scroll view. To download the full example demonstrating these methods, download the LineRange Example.
[ Developers Corner| Home| Products| Ordering| Support| Trademarks ]

Objective Technologies Inc.
90-07 68th Ave., Forest Hills, NY 11375
email: voice: 718.997.9741
Developers Corner: Line Range - Copyright © 1995-2008 Objective Technologies, Inc. ® All rights reserved.

NOTE: OTI does not warrant, guarantee, or otherwise assume any responsibility or accountability for any loss or corruption caused by any of the software or suggestions mentioned or available on this page. If something bad happens as a result of using this software, it's your own fault for using it. Please report any problems to