拇指大爆炸-分词功能

最近看到iOS/Mac 开发者钟颖 Cyan 和锤子科技关于是否抄袭的争议,有感而发:这是iOS在分词功能的实现。并没有关于俩这进行谈论。

如何实现拇指大爆炸的功能

1:使用3D Touch来展示内容,这部分简单的说明:

1.1 是否有3D Touch功能

//方法里检测手机是否有3D Touch功能

  • (void)viewWillAppear:(BOOL)animated {
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    //有3D Touch功能
    [self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
    }else{
    //没有3D Touch功能
    }
    }
1.2 实现3D Touch的代理方法UIViewControllerPreviewingDelegate

pragma mark - 3D Touch Delegate

//点击进入预览模式: 实现该协议方法

  • (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location {
    // check if we're not already displaying a preview controller
    if ([self.presentedViewController isKindOfClass:[WJFTextVC class]]) {
    return nil;
    }
    WJFTextVC *previewController = [WJFTextVC new];
    previewController.view.backgroundColor = [UIColor blueColor];
    previewController.textArray = self.words;
    return previewController;
    }

//继续按压进入:实现该协议

  • (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    WJFTextVC *previewController = [WJFTextVC new];
    previewController.view.backgroundColor = [UIColor redColor];
    previewController.textArray = self.words;
    [self showViewController:previewController sender:self];

}

2 重点分词功能的实现(采用系统自带的api)

-(NSArray *)stringTokenizerWithWord:(NSString *)word{
NSMutableArray *keyWords = [NSMutableArray new];
CFStringTokenizerRef ref = CFStringTokenizerCreate(NULL, (__bridge CFStringRef)word, CFRangeMake(0, word.length),kCFStringTokenizerUnitWord,NULL);
CFRange range;
CFStringTokenizerAdvanceToNextToken(ref);
range=CFStringTokenizerGetCurrentTokenRange(ref);
NSString *keyWord;
while (range.length>0)
{
keyWord=[word substringWithRange:NSMakeRange(range.location, range.length)];
[keyWords addObject:keyWord];
CFStringTokenizerAdvanceToNextToken(ref);
range=CFStringTokenizerGetCurrentTokenRange(ref);
}
return keyWords;
}

你可能感兴趣的:(拇指大爆炸-分词功能)