7 #import <Foundation/Foundation.h>
8 #import <UIKit/UIKit.h>
10 #import "flutter/fml/logging.h"
18 @property(nonatomic, retain) UITextChecker* textChecker;
29 _textChecker = [[UITextChecker alloc] init];
31 NSString* method = call.
method;
34 FML_DCHECK(args.count == 2);
35 id language = args[0];
37 if (language == [NSNull
null] || text == [NSNull
null]) {
43 NSArray<NSDictionary<NSString*, id>*>* spellCheckResult =
44 [
self findAllSpellCheckSuggestionsForText:text inLanguage:language];
45 result(spellCheckResult);
62 - (NSArray<NSDictionary<NSString*, id>*>*)findAllSpellCheckSuggestionsForText:(NSString*)text
63 inLanguage:(NSString*)language {
65 if ([language containsString:
@"-"]) {
66 NSArray<NSString*>* languageCodes = [language componentsSeparatedByString:@"-"];
67 FML_DCHECK(languageCodes.count == 2);
68 NSString* lastCode = [[languageCodes lastObject] uppercaseString];
69 language = [NSString stringWithFormat:@"%@_%@", [languageCodes firstObject], lastCode];
72 if (![UITextChecker.availableLanguages containsObject:language]) {
76 NSMutableArray<FlutterSpellCheckResult*>* allSpellSuggestions = [[NSMutableArray alloc] init];
79 NSUInteger nextOffset = 0;
81 nextSpellSuggestion = [
self findSpellCheckSuggestionsForText:text
83 startingOffset:nextOffset];
84 if (nextSpellSuggestion != nil) {
85 [allSpellSuggestions addObject:nextSpellSuggestion];
87 nextSpellSuggestion.
misspelledRange.location + nextSpellSuggestion.misspelledRange.length;
89 }
while (nextSpellSuggestion != nil && nextOffset < text.length);
91 NSMutableArray* methodChannelResult = [[[NSMutableArray alloc] init] autorelease];
94 [methodChannelResult addObject:[result toDictionary]];
97 [allSpellSuggestions release];
98 return methodChannelResult;
105 inLanguage:(NSString*)language
106 startingOffset:(NSInteger)startingOffset {
107 FML_DCHECK([UITextChecker.availableLanguages containsObject:language]);
108 NSRange misspelledRange =
109 [
self.textChecker rangeOfMisspelledWordInString:text
110 range:NSMakeRange(0, text.length)
111 startingAt:startingOffset
114 if (misspelledRange.location == NSNotFound) {
121 NSArray<NSString*>* suggestions = [
self.textChecker guessesForWordRange:misspelledRange
126 suggestions:suggestions] autorelease];
130 - (UITextChecker*)textChecker {
135 [_textChecker release];
143 - (instancetype)initWithMisspelledRange:(NSRange)range
144 suggestions:(NSArray<NSString*>*)suggestions {
147 _suggestions = [suggestions copy];
148 _misspelledRange = range;
154 NSMutableDictionary* result = [[[NSMutableDictionary alloc] initWithCapacity:3] autorelease];
155 result[@"startIndex"] = @(_misspelledRange.location);
158 result[@"endIndex"] = @(_misspelledRange.location + _misspelledRange.length);
159 result[@"suggestions"] = _suggestions;
164 [_suggestions release];