我有一个UIPopover,显示一个包含UITextView的普通视图,其中包含一些文本.我设法强调了文字.当弹出窗口被解除并重新打开时,突出显示消失.即使应用程序已关闭,我也希望保持文本突出显示.任何想法如何实现?我使用的代码如下:
- (void)highlight { NSRange selectedRange = self.textViewAll.selectedRange; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.textViewAll.attributedText]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange]; // [highlightedRange addobject:]; // This is where i tried to save each location and length in a mutable array but didn't work [highlightedRangeLocation insertObject:[NSNumber numberWithInteger:selectedRange.location] atIndex:indexOfHighlight]; [highlightedRangeLength insertObject:[NSNumber numberWithInteger:selectedRange.length] atIndex:indexOfHighlight]; /////////////////////////////////////////////////////////////////////////////// self.textViewAll.attributedText = attributedString; indexOfHighlight ++ ; } - (void)viewDidLoad { UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highlight)]; [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]]; float sysver = [[[UIDevice currentDevice] systemVersion] floatValue]; if (sysver >= 8.0) { self.textViewAll.layoutManager.allowsNonContiguousLayout = NO; } }
谁能指出如何从这里继续?
编辑1:
- (IBAction)closeFun:(id)sender { // self.popoverPresentationController set [self dismissViewControllerAnimated:YES completion:nil]; // [self dismis] }
解决方法
每当弹出窗口被解除时,你不能在[NSUserDefaults standardUserDefaults]中保存突出显示的文本范围,并在弹出窗口重新出现时检索它吗?