3

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:

Setected text in <code>UITextView</code>

  • Highlighted text in UITextView with a new background color, chosen by the user after tapping on the highlight feature: Highlighted text in <code>UITextView</code> with a new background color

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?

neowinston
  • 7,584
  • 10
  • 52
  • 83

2 Answers2

7

I ended up solving my problem like this, and if someone else has a more elegant solution please let me know:

- (void)viewDidLoad {

    [super 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.textView.layoutManager.allowsNonContiguousLayout = NO;
    } 
}

- (void)highlight {

    NSRange selectedTextRange = self.textView.selectedRange;

    [attributedString addAttribute:NSBackgroundColorAttributeName
                             value:[UIColor redColor]
                             range:selectedTextRange];

    float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (sysVer < 8.0) {
        // iOS 7 fix
        self.textView.scrollEnabled = NO;
        self.textView.attributedText = attributedString;
        self.textView.scrollEnabled = YES;
    } else {
        self.textView.attributedText = attributedString;
    }
}
neowinston
  • 7,584
  • 10
  • 52
  • 83
  • thank you so much!!! i tried to fix this issue with: textView.scrollEnabled = false ... textView.scrollEnabled = true but that didn't work in ios 8. just added self.textView.layoutManager.allowsNonContiguousLayout = false and everything is fine – schirrmacher Nov 23 '14 at 18:33
  • In my app, disabling scrollEnabled still prevented the jumping in iOS 8, but any attempt to reenable it had no effect, so the scrolling became permanently disabled after the first edit. Disabling allowsNonContiguousLayout instead avoided that problem. I couldn't tell if there were any disadvantages to doing that, so I reenabled it after editing just in case. – arlomedia Mar 12 '15 at 21:33
0

move self.textView.scrollEnabled = NO; to the first line of highlight method.

Hope it helps!

ZAZ
  • 597
  • 3
  • 6
  • Thanks for your answer ZAZ, but it didn't work. I'm still having the same problem. – neowinston Oct 19 '14 at 19:08
  • is it scrolling to another location or comes back to its original position? if u can don one thing comment this `self.textView.attributedText = attributedString;` and then check the response. i know tht doing this vil not highlight text. – ZAZ Oct 19 '14 at 19:21
  • I've tried your suggestion and is it scrolling to another location, more specifically to the beginning of the UITextView. And if I select and highlight at beginning of the text it won't scroll anymore. – neowinston Oct 19 '14 at 19:46