【问题标题】:UITextView text selection and highlight jumping in iOS 8iOS 8 中的 UITextView 文本选择和高亮跳转
【发布时间】:2014-12-14 17:59:57
【问题描述】:

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

  • UITextView 中设置文本,用户可以使用highlight 功能:

  • UITextView 中带有新背景颜色的突出显示文本,由用户在点击 highlight 功能后选择:

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;
}

但在 iOS 8 中,文本选择是跳跃的。当我使用 UIMenuItemUIMenuController 中的 highlight 功能时,它还会跳转到另一个 UITextView 偏移量。

如何在 iOS 8 中解决这个问题?

【问题讨论】:

    标签: ios iphone ios7 ios8 uitextview


    【解决方案1】:

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

    - (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;
        }
    }
    

    【讨论】:

    • 非常感谢!!!我试图解决这个问题: textView.scrollEnabled = false ... textView.scrollEnabled = true 但这在 ios 8 中不起作用。刚刚添加了 self.textView.layoutManager.allowsNonContiguousLayout = false 一切都很好
    • 太棒了。我很高兴能帮上忙。
    • 在我的应用程序中,禁用 scrollEnabled 仍然阻止了 iOS 8 中的跳转,但任何重新启用它的尝试都没有效果,因此在第一次编辑后滚动永久禁用。禁用allowsNonContiguousLayout 反而避免了这个问题。我不知道这样做是否有任何缺点,所以我在编辑后重新启用它以防万一。
    【解决方案2】:

    移动 self.textView.scrollEnabled = NO;到高亮方法的第一行。

    希望对你有帮助!

    【讨论】:

    • 感谢您的回答 ZAZ,但它不起作用。我仍然遇到同样的问题。
    • 是滚动到另一个位置还是回到原来的位置?如果你能做一件事评论这个self.textView.attributedText = attributedString;然后检查回复。我知道这样做不会突出显示文本。
    • 我已经尝试过你的建议,它是否滚动到另一个位置,更具体地说是 UITextView 的开头。如果我在文本的开头选择并突出显示,它将不再滚动。
    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 2011-12-09
    • 2010-11-02
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    相关资源
    最近更新 更多