您可以使用 textview 的布局管理器来获取这些内容。但它在 iOS 7 中可用。您可以使用布局管理器的方法 - ( enumerateLineFragmentsForGlyphRange:usingBlock: )
下面的代码打印结果如图所示
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);
}]
;
打印结果
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 -
因此,在该块的每次运行中,您将获得 glymphRange.length 作为该行中使用的字符串的长度。