UITextView文本选择和在iOS 8中突出显示跳跃

Win*_*ton 3 iphone uitextview ios ios7 ios8

我正在使用UIMenuItemUIMenuController为我添加一个高亮功能UITextView,因此用户可以更改所选文本的背景颜色,如下图所示:

  • 使用用户可用UITextView突出显示功能检测文本:

在<code>UITextView</code>使用新的背景颜色突出显示的文本,用户在点按<strong>突出显示</strong>功能后选择:
<img rel=

Win*_*ton 7

我最终解决了这个问题,如果其他人有更优雅的解决方案,请告诉我:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)