I have uitextview with text. I want to read it line by line. I want to it for up to iOS 5 and without - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
delegate method. How can I do that?
You can use the textview's layout manager to get those. But it is available from iOS 7. You can use layout manager's method - ( enumerateLineFragmentsForGlyphRange:usingBlock: )
The below code prints the result as shown
textView.text =@"abcdsakabsbdkjflk sadjlkasjdlk asjkdasdklj asdpaandjs bajkhdb hasdskjbnas kdbnkja sbnkasj dbkjasd kjk aj";
NSLog(@"%d",_textView.text.length); // length here is 104.
[_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, 104) usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) {
NSLog(@"rect %@ - usedRect %@ - glymph Rangle %d %d -",NSStringFromCGRect(rect),NSStringFromCGRect(usedRect),glyphRange.location,glyphRange.length);
}]
;
print result
2013-12-17 12:48:40.250 testEmpty[675:a0b] rect {{0, 0}, {200, 13.8}} - usedRect {{0, 0}, {176.08398, 13.8}} - glymph Rangle 0 31 -
2013-12-17 12:48:40.251 testEmpty[675:a0b] rect {{0, 13.8}, {200, 13.8}} - usedRect {{0, 13.8}, {182.11328, 13.8}} - glymph Rangle 31 31 -
2013-12-17 12:48:40.251 testEmpty[675:a0b] rect {{0, 27.6}, {200, 13.8}} - usedRect {{0, 27.6}, {168.75977, 13.8}} - glymph Rangle 62 28 -
2013-12-17 12:48:40.252 testEmpty[675:a0b] rect {{0, 41.400002}, {200, 13.8}} - usedRect {{0, 41.400002}, {82.035156, 13.8}} - glymph Rangle 90 14 -
So in each run of the block, you will get the glymphRange.length as length of the string used in that line.
Use:
NSArray *lines = [textView.text componentsSeparatedByString:@"\n"];
Your array has the lines, each line is in an index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With