UITextView text selection and highlight jumping in iOS 8

后端 未结 2 1415
礼貌的吻别
礼貌的吻别 2020-12-06 22:06

I\'m using UIMenuItem and UIMenuController to add a highlight feature to my UITextView, so the user can change the

相关标签:
2条回答
  • 2020-12-06 22:47

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 22:48

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

    Hope it helps!

    0 讨论(0)
提交回复
热议问题