7 #import <Foundation/Foundation.h>
8 #import <UIKit/UIKit.h>
10 #import "flutter/fml/logging.h"
19 @property(nonatomic, copy, readonly) NSArray<NSString*>*
suggestions;
24 - (instancetype)initWithMisspelledRange:(NSRange)range
25 suggestions:(NSArray<NSString*>*)suggestions NS_DESIGNATED_INITIALIZER;
32 @property(nonatomic) UITextChecker* textChecker;
39 if (!
self.textChecker) {
43 self.textChecker = [[UITextChecker alloc] init];
45 NSString* method = call.
method;
48 FML_DCHECK(args.count == 2);
49 id language = args[0];
51 if (language == [NSNull
null] || text == [NSNull
null]) {
57 NSArray<NSDictionary<NSString*, id>*>* spellCheckResult =
58 [
self findAllSpellCheckSuggestionsForText:text inLanguage:language];
59 result(spellCheckResult);
76 - (NSArray<NSDictionary<NSString*, id>*>*)findAllSpellCheckSuggestionsForText:(NSString*)text
77 inLanguage:(NSString*)language {
79 if ([language containsString:
@"-"]) {
80 NSArray<NSString*>* languageCodes = [language componentsSeparatedByString:@"-"];
81 FML_DCHECK(languageCodes.count == 2);
82 NSString* lastCode = [[languageCodes lastObject] uppercaseString];
83 language = [NSString stringWithFormat:@"%@_%@", [languageCodes firstObject], lastCode];
86 if (![UITextChecker.availableLanguages containsObject:language]) {
90 NSMutableArray<FlutterSpellCheckResult*>* allSpellSuggestions = [[NSMutableArray alloc] init];
93 NSUInteger nextOffset = 0;
95 nextSpellSuggestion = [
self findSpellCheckSuggestionsForText:text
97 startingOffset:nextOffset];
98 if (nextSpellSuggestion != nil) {
99 [allSpellSuggestions addObject:nextSpellSuggestion];
103 }
while (nextSpellSuggestion != nil && nextOffset < text.length);
105 NSMutableArray* methodChannelResult =
106 [[NSMutableArray alloc] initWithCapacity:allSpellSuggestions.count];
109 [methodChannelResult addObject:[result toDictionary]];
112 return methodChannelResult;
119 inLanguage:(NSString*)language
120 startingOffset:(NSInteger)startingOffset {
121 FML_DCHECK([UITextChecker.availableLanguages containsObject:language]);
123 [
self.textChecker rangeOfMisspelledWordInString:text
124 range:NSMakeRange(0, text.length)
125 startingAt:startingOffset
135 NSArray<NSString*>*
suggestions = [
self.textChecker guessesForWordRange:misspelledRange
139 suggestions:suggestions];
146 - (instancetype)initWithMisspelledRange:(NSRange)range
147 suggestions:(NSArray<NSString*>*)suggestions {
150 _suggestions = [suggestions copy];
151 _misspelledRange = range;
158 @"startIndex" : @(_misspelledRange.location),
162 @"endIndex" : @(_misspelledRange.location + _misspelledRange.length),
163 @"suggestions" : _suggestions,