iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了UITextView文本选择和在iOS 8中突出显示跳跃大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用UIMenuItem和UIMenuController为我的UITextView添加一个高亮功能,因此用户可以更改所选文本的背景颜色,如下图所示:

> UITextView中的Setected文本,具有用户可用的突出显示功能

> UITextView中突出显示的文本,使用新的背景颜色,用户在点击突出显示功能后选择:

在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中,文本选择正在跳跃.当我使用UIMenuItem和UIMenuController中的突出显示功能时,它也会跳转到另一个UITextView偏移量.

如何在iOS 8中解决此问题?

解决方法

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

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

大佬总结

以上是大佬教程为你收集整理的UITextView文本选择和在iOS 8中突出显示跳跃全部内容,希望文章能够帮你解决UITextView文本选择和在iOS 8中突出显示跳跃所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。