Counting Lines of Text
1.Counting hard line breaks
NSString *string;
unsigned numberOfLines, index, stringLength = [string length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++){
index = NSMaxRange([string lineRangeForRange:NSMakeRange(index, 0)]);
}
2.Counting lines of wrapped text
NSLayoutManager *layoutManager = [textView layoutManager];
unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
NSRange lineRange;
for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++){
(void) [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:&lineRange];
index = NSMaxRange(lineRange);
}
Reference:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/TextLayout.pdf