Saturday, May 18, 2013

NSString lineRangeForRange example ios


[NSString lineRangeForRange]

Returns the range of characters representing the line or lines containing a given range.
- (NSRange)lineRangeForRange:(NSRange)aRange
Parameters
aRange
A range within the receiver.
Return Value of [NSString lineRangeForRange]
The range of characters representing the line or lines containing aRange, including the line termination characters.
Example of [NSString lineRangeForRange]
NSString *string = @"tjykluytjghklukytgjhkkghkj sdkjlhfkjsadgfiulgeje fuaeyfkjasdgfueghf aksjgflkj. wyruehskjluishfoeifh uasyeajhkfa uiyelkjahsdf uayekljshdf aehkfjsd. \n I iheio;fajkdsf sdfhlueshkfjskdhf ujhelkjfh. luehljkfhlsdf. leufhlkjdshfa. \n euoiywhfldsjkhf euyhfsdlkj. ewhlkjfsd. euilhfsdkjishdkjf euhjklsfd. \n";
NSLog(@"string length:%i", [string length]);
NSRange range;
range.length = [string length]/2;
range.location = 0;
NSLog(@"LineRangeForRange:%i", [string lineRangeForRange:range].length);
NSLog(@"Substring:%@", [string substringWithRange:[string lineRangeForRange:range]]);
Example of [NSString lineRangeForRange]
NSString *string;
unsigned numberOfLines, index, stringLength = [string length];

for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);
Example of [NSString lineRangeForRange]
NSRange startRange = NSMakeRange(0, 1);
NSRange titleRange = [noteTextView.text lineRangeForRange:startRange];  
NSString *titleString = [noteTextView.text substringToIndex:titleRange.length];
NSLog(@"The title is: %@", titleString);