I'm using UIMenuItem
and UIMenuController
to add a highlight feature to my UITextView
, so the user can change the background color of the selected text, as shown in the pictures bellow:
- Setected text in
UITextView
with the highlight feature available to the user:
- Highlighted text in
UITextView
with a new background color, chosen by the user after tapping on the highlight feature:
In iOS 7 the following code is working perfectly to accomplish this task:
- (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;
}
But in iOS 8 the text selection is jumping. When I use the highlight feature from the UIMenuItem
and UIMenuController
it jumps also to another UITextView
offset.
How do I solve this problem in iOS 8?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…