在UITextView中查找可见文本的范围
我从以前的问题的答案中find代码,计算UITextView中可见的属性文本的范围。 它完美的作品,除了我添加任何types的换行符以创build一个新的段落。 在这种情况下,代码给出了不正确的答案,直到我手动滚动视图。 滚动后,每次都给出正确的答案。 但是,即使没有滚动,我也需要它给出正确的答案。
以下是我的示例项目。 这是一个单一的视图应用程序。 程序在logging中报告它认为可见的文本,在视图加载之后的第一次,然后在滚动之后。
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIScrollViewDelegate> { UITextView *lTextView; } @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; int statusBarHeight = 20; UIScrollView *scrollView; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight, self.view.frame.size.width * 2, self.view.frame.size.height)]; scrollView.pagingEnabled = YES; scrollView.delegate = self; [self.view addSubview:scrollView]; lTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width/2, scrollView.frame.size.height)]; lTextView.editable = NO; lTextView.selectable = NO; lTextView.attributedText = [self loremIpsum]; [scrollView addSubview:lTextView]; lTextView.delegate = self; [self report]; } - (void)report { NSArray *visibleRange = [self visibleRangeOfTextView:lTextView]; NSNumber *start = [visibleRange objectAtIndex:0]; NSNumber *end = [visibleRange objectAtIndex:1]; int rangeLength = (end.intValue - start.intValue); NSLog(@"%@", [lTextView.text substringWithRange:NSMakeRange(start.intValue, rangeLength)]); } -(NSArray *)visibleRangeOfTextView:(UITextView *)textView { CGRect bounds = textView.bounds; bounds.size.height -= 30.0; // to prevent including lines that can barely be seen at the bottom UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start; UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end; // <<<< this gives an incorrect calculation until the user manually scrolls float startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:start]; float endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:end]; NSLog(@"startOffset: %f, endOffset %f", startOffset, endOffset); return [NSArray arrayWithObjects:[NSNumber numberWithFloat:startOffset], [NSNumber numberWithFloat:endOffset], nil]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"scrollViewDidEndDecelerating"); [self report]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { NSLog(@"scrollViewDidEndDragging"); [self report]; } - (NSAttributedString *)loremIpsum { NSMutableAttributedString *li = [[NSMutableAttributedString alloc] init]; NSArray *strings = [NSArray arrayWithObjects:@"1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam congue eleifend risus eget pretium. Donec sed commodo neque, id ornare dolor.", @" 2 Vivamus vestibulum non quam et euismod. Morbi et dolor luctus velit lobortis ornare vel vel tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean viverra, urna nec tempor commodo, turpis nisl rhoncus mauris, in ullamcorper justo sapien quis nulla.", @" 3 Aliquam at odio molestie, laoreet elit sed, suscipit risus. Nulla eleifend, quam eget porttitor condimentum, metus lacus lobortis ligula, accumsan tristique neque turpis non purus. Aenean malesuada tortor id elit semper, et pretium nulla viverra.", @" 4 Aliquam sollicitudin placerat massa, quis posuere est ornare vel. Nam mollis convallis risus a tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.", @" 5 Fusce facilisis orci nisi, vel egestas metus tristique vitae. Proin nec malesuada dolor. Donec eget felis accumsan, facilisis turpis vitae, blandit lacus.", @" 6 Pellentesque auctor nisl quis turpis commodo lacinia. In sed euismod urna. Praesent sed commodo magna.", @" 7 Ut interdum dignissim urna, nec feugiat dolor. Nulla facilisi. Donec fermentum mauris at ante tincidunt, id accumsan eros lacinia.", @" 8 Suspendisse potenti. Integer ac mattis eros, sed volutpat dui. Pellentesque vehicula turpis ut metus malesuada blandit.", @" 9 Nam laoreet dui id imperdiet pulvinar. In auctor enim ac massa feugiat adipiscing. Nam convallis neque at felis tincidunt iaculis. Maecenas dictum est ac nulla suscipit, nec condimentum metus molestie.", @" 10 Vestibulum mollis velit eu nunc eleifend egestas. Ut aliquam ultrices tellus volutpat consectetur. Morbi eget sollicitudin quam, ut imperdiet leo. Morbi sed ligula iaculis, tincidunt diam nec, pharetra ligula.", @" 11 Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin varius facilisis placerat. Fusce suscipit risus risus, in varius diam convallis quis. In hac habitasse platea dictumst. Integer non lectus non dolor fringilla venenatis eget quis nisl. Proin in pretium metus.", @" 12 Pellentesque sed tellus iaculis, bibendum neque vel, porta ante. Phasellus eu vulputate massa. Nullam venenatis lectus non nunc aliquet porta. Nunc gravida rutrum feugiat. Fusce elit nunc, facilisis non tristique placerat, tempor id orci. Mauris et massa cursus, dapibus urna a, condimentum arcu. Pellentesque vitae sagittis sapien. Ut lacus purus, suscipit at magna non, rhoncus luctus dui. Ut ipsum augue, pharetra ac ipsum sed, facilisis convallis justo.", @" 13 Ut arcu augue, hendrerit vel tincidunt vitae, aliquet ac quam. Nulla ullamcorper, dolor eu pellentesque cursus, lectus quam interdum ante, nec congue dui augue nec dolor. Sed convallis elit in enim dictum, at posuere sem mollis. Praesent in metus aliquam, ullamcorper purus tempor, mattis ipsum. Aliquam gravida, sem vitae iaculis placerat, dui velit commodo nulla, vitae ultrices lectus dolor ut mi.", @" 14 Suspendisse quis metus varius, congue turpis vitae, viverra nunc. Duis placerat, felis et laoreet pretium, nibh lorem pulvinar turpis, eu euismod arcu libero at mauris. Sed laoreet, eros in tempor accumsan, odio augue fermentum dui, a pharetra felis libero eu ligula.", @"Suspendisse ultricies pulvinar urna. Donec placerat nulla non elit vestibulum mattis.", @"Phasellus semper sem a sem dignissim tempus. Sed scelerisque sed purus interdum rhoncus. Sed gravida eros sit amet dui fermentum rutrum. Proin a arcu scelerisque, volutpat orci quis, dignissim neque. Aliquam erat volutpat. Sed eget scelerisque neque.", nil]; for (int i = 0; i < strings.count; i++) { NSString *string = [strings objectAtIndex:i]; if (i != 0) { // Comment this line and it will work perfectly: [li appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]]; } [li appendAttributedString:[[NSAttributedString alloc] initWithString:string]]; } return li; } @end
感谢您的帮助!
我在添加奖金的那一天提出了一个解决scheme,但没有发布,以便其他人有机会发布对他人有帮助的东西。 巴乔雷克先生和林塔罗先生都发表了有效的答案。 我的解决scheme有点不同, 它更适合我在我的项目中要做的事情(因为我将使用多个UITextViews)。 它禁用UITextView中的滚动,让UIScrollView处理滚动,并添加UITextView所需的全部高度,以容纳所有的属性文本。 这使得计算从一开始就能正确工作。
为了以后可以阅读的其他人的利益,我在下面发布一个完整的工作示例。 我会把这个标记为公认的答案,因为这是我发布其他解决scheme之前的路线。
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIScrollViewDelegate> { UITextView *lTextView; UIScrollView *scrollView; } @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; int statusBarHeight = 20; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, statusBarHeight, self.view.frame.size.width, self.view.frame.size.height)]; [self.view addSubview:scrollView]; lTextView = [[UITextView alloc] init]; lTextView.editable = NO; lTextView.selectable = NO; lTextView.scrollEnabled = NO; lTextView.textContainerInset = UIEdgeInsetsZero; lTextView.attributedText = [self loremIpsum]; float calculatedHeight = [self heightForTextViewWithAttributedText:lTextView andWidth:scrollView.frame.size.width]; lTextView.frame = CGRectMake(0, 0, scrollView.frame.size.width, calculatedHeight); [scrollView addSubview:lTextView]; [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, calculatedHeight + 20)]; scrollView.delegate = self; [self report]; } - (void)report { NSArray *visibleRange = [self visibleRangeOfTextView:lTextView]; NSNumber *start = [visibleRange objectAtIndex:0]; NSNumber *end = [visibleRange objectAtIndex:1]; int rangeLength = (end.intValue - start.intValue); NSLog(@"%@", [lTextView.text substringWithRange:NSMakeRange(start.intValue, rangeLength)]); } -(NSArray *)visibleRangeOfTextView:(UITextView *)textView { double offsetY = scrollView.contentOffset.y; CGPoint startingPoint = scrollView.contentOffset; startingPoint.y += 5; // to prevent including lines that can barely be seen at the top CGPoint endingPoint = CGPointMake(320, offsetY + scrollView.frame.size.height - 20); CGRect bounds = textView.bounds; bounds.size.height -= 30.0; // to prevent including lines that can barely be seen at the bottom UITextPosition *start = [textView characterRangeAtPoint:startingPoint].start; UITextPosition *end = [textView characterRangeAtPoint:endingPoint].end; float startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:start]; float endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:end]; return [NSArray arrayWithObjects:[NSNumber numberWithFloat:startOffset], [NSNumber numberWithFloat:endOffset], nil]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"scrollViewDidEndDecelerating"); [self report]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { NSLog(@"scrollViewDidEndDragging"); [self report]; } - (NSAttributedString *)loremIpsum { NSMutableAttributedString *li = [[NSMutableAttributedString alloc] init]; NSArray *strings = [NSArray arrayWithObjects:@"1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam congue eleifend risus eget pretium. Donec sed commodo neque, id ornare dolor.", @" 2 Vivamus vestibulum non quam et euismod. Morbi et dolor luctus velit lobortis ornare vel vel tellus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean viverra, urna nec tempor commodo, turpis nisl rhoncus mauris, in ullamcorper justo sapien quis nulla.", @" 3 Aliquam at odio molestie, laoreet elit sed, suscipit risus. Nulla eleifend, quam eget porttitor condimentum, metus lacus lobortis ligula, accumsan tristique neque turpis non purus. Aenean malesuada tortor id elit semper, et pretium nulla viverra.", @" 4 Aliquam sollicitudin placerat massa, quis posuere est ornare vel. Nam mollis convallis risus a tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.", @" 5 Fusce facilisis orci nisi, vel egestas metus tristique vitae. Proin nec malesuada dolor. Donec eget felis accumsan, facilisis turpis vitae, blandit lacus.", @" 6 Pellentesque auctor nisl quis turpis commodo lacinia. In sed euismod urna. Praesent sed commodo magna.", @" 7 Ut interdum dignissim urna, nec feugiat dolor. Nulla facilisi. Donec fermentum mauris at ante tincidunt, id accumsan eros lacinia.", @" 8 Suspendisse potenti. Integer ac mattis eros, sed volutpat dui. Pellentesque vehicula turpis ut metus malesuada blandit.", @" 9 Nam laoreet dui id imperdiet pulvinar. In auctor enim ac massa feugiat adipiscing. Nam convallis neque at felis tincidunt iaculis. Maecenas dictum est ac nulla suscipit, nec condimentum metus molestie.", @" 10 Vestibulum mollis velit eu nunc eleifend egestas. Ut aliquam ultrices tellus volutpat consectetur. Morbi eget sollicitudin quam, ut imperdiet leo. Morbi sed ligula iaculis, tincidunt diam nec, pharetra ligula.", @" 11 Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin varius facilisis placerat. Fusce suscipit risus risus, in varius diam convallis quis. In hac habitasse platea dictumst. Integer non lectus non dolor fringilla venenatis eget quis nisl. Proin in pretium metus.", @" 12 Pellentesque sed tellus iaculis, bibendum neque vel, porta ante. Phasellus eu vulputate massa. Nullam venenatis lectus non nunc aliquet porta. Nunc gravida rutrum feugiat. Fusce elit nunc, facilisis non tristique placerat, tempor id orci. Mauris et massa cursus, dapibus urna a, condimentum arcu. Pellentesque vitae sagittis sapien. Ut lacus purus, suscipit at magna non, rhoncus luctus dui. Ut ipsum augue, pharetra ac ipsum sed, facilisis convallis justo.", @" 13 Ut arcu augue, hendrerit vel tincidunt vitae, aliquet ac quam. Nulla ullamcorper, dolor eu pellentesque cursus, lectus quam interdum ante, nec congue dui augue nec dolor. Sed convallis elit in enim dictum, at posuere sem mollis. Praesent in metus aliquam, ullamcorper purus tempor, mattis ipsum. Aliquam gravida, sem vitae iaculis placerat, dui velit commodo nulla, vitae ultrices lectus dolor ut mi.", @" 14 Suspendisse quis metus varius, congue turpis vitae, viverra nunc. Duis placerat, felis et laoreet pretium, nibh lorem pulvinar turpis, eu euismod arcu libero at mauris. Sed laoreet, eros in tempor accumsan, odio augue fermentum dui, a pharetra felis libero eu ligula.", @"Suspendisse ultricies pulvinar urna. Donec placerat nulla non elit vestibulum mattis.", @"Phasellus semper sem a sem dignissim tempus. Sed scelerisque sed purus interdum rhoncus. Sed gravida eros sit amet dui fermentum rutrum. Proin a arcu scelerisque, volutpat orci quis, dignissim neque. Aliquam erat volutpat. Sed eget scelerisque neque.", nil]; for (int i = 0; i < strings.count; i++) { NSString *string = [strings objectAtIndex:i]; if (i != 0) { // Comment this line and it will work perfectly: [li appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]]; } [li appendAttributedString:[[NSAttributedString alloc] initWithString:string]]; } return li; } - (CGFloat)heightForTextViewWithAttributedText:(UITextView *)textView andWidth:(CGFloat)width { CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)]; return size.height + 3.0; } @end
这个问题是由于尽快找出可见范围造成的。
UI更改的实际效果并不总是立即发生。 相反,UIKit可以延迟一些工作,直到完成主线程。
在提供的示例代码中,将lTextView
添加到scrollView
,然后在相同的执行线程上计算visibleRange 。 那额外的UIKit工作尚未完成。
将[self report]
调用asynchronous分发回主线程。 ViewDidLoad
将完成,放弃主线程。 UIKit将有机会完成,你现在应该得到正确的可见范围 。
在viewDidLoad
,更改:
[self report];
至:
dispatch_async(dispatch_get_main_queue(), ^{ [self report]; });
在- viewDidLoad
, self.view
实际上并不在视图层次结构中。
因此, UITextView
的- drawRect
还没有调用。
我认为这就是为什么- offsetFromPosition:
无法计算。
尝试这个:
- (void)viewDidAppear:(BOOL)animated { NSLog(@"viewDidAppear"); [self report]; }
如果您确实想要立即强制UITextView
布局,请尝试:
[[lTextView layoutManager] ensureLayoutForTextContainer:[lTextView textContainer]];
听起来像API中的UI信息不是最新的。 只要您滚动UI信息更新。 你正在寻找的是一种方法,一旦它被改变,检索新的数据。 尝试调用layoutIfNeeded,然后尝试找出可见的属性文本的范围。
在委托函数“textView:didEndEdited:”中调用“report”
-(void)textView:(UITextView *)textview didEndEdited{ [self report];}
当然你已经设置了TextView的委托:
self.textView.delegate = self;
并声明委托协议。