Tag: ios8 uitextview

UITextView文本select和高亮在iOS 8中跳跃

我正在使用UIMenuItem和UIMenuController向我的UITextView添加高亮function,因此用户可以更改所选文本的背景颜色 ,如下图所示: 在UITextView使用高亮function在用户中检测到文本: UITextView突出显示的文本具有新的背景色,用户在点击突出显示的特征后select: 在iOS 7中 ,以下代码正在完美地完成此任务: – (void)viewDidLoad { [super viewDidLoad]; UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highlight)]; [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:highlightMenuItem]]; } – (void)highlight { NSRange selectedTextRange = self.textView.selectedRange; [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:selectedTextRange]; // iOS 7 fix, NOT working in iOS 8 self.textView.scrollEnabled = NO; self.textView.attributedText = attributedString; self.textView.scrollEnabled = YES; } […]