可以以编程方式提供iOS预测键盘上下文/源文本?

前端之家收集整理的这篇文章主要介绍了可以以编程方式提供iOS预测键盘上下文/源文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个消息应用程序,并希望iOS 8中的预测键盘能够识别正在撰写消息的用户正在回复上一个消息.

所以我希望能够将一个字符串提供给键盘,为它提供预测的上下文.因此,如果用户被问及一个可以解释为极性的问题(是/否),那么预测键盘应该是“是”不|也许

这是开发人员可以使用的吗?

请注意,我不是在说自定义键盘,只是给标准键盘提供了一些上下文,因为它是预测的.我也不关心实际定制快速类型的回复as in this question.我只是希望键盘知道它是什么打字.

解决方法

严格地将建议输入到默认键盘是不可能的.但是,如果您想为用户提供相同的体验,我将使用myTextView.autocorrectionType = UITextAutocorrectionTypeNo隐藏iOS建议栏;然后用我自己的自定义视图替换该视图,模仿建议视图直到.一旦用户键入字符或选择了一个选项,然后隐藏自定义建议视图并重新启用iOS建议栏.

我的子类UIInputView只是为了这个(透明度和转换是一点点,但一切都很好).

  1. #import <UIKit/UIKit.h>
  2.  
  3. @protocol SuggestionViewDelegate <NSObject>
  4.  
  5. @required
  6. - (void)suggestionSelected:(NSString *)suggestion;
  7.  
  8. @end
  9.  
  10. @interface SuggestionView : UIInputView
  11.  
  12. - (instancetype)init;
  13. - (instancetype)initWithFrame:(CGRect)frame;
  14.  
  15. /**
  16. * The list of suggestions being displayed.
  17. * The array contains 0-3 strings.
  18. *
  19. * @return Array of NSString's representing the current suggested strings
  20. */
  21. - (NSArray *)suggestions;
  22.  
  23. /**
  24. * Add a suggestion to display in the view.
  25. * If there are already maxSuggestionCount suggestions,the added suggestion will push one of them out.
  26. * If there are already maxSuggestionCount suggestions and the input is 'nil' then the last suggestion will be removed.
  27. *
  28. * @param suggestion String to suggest to the user
  29. */
  30. - (void)addSuggestion:(NSString *)suggestion;
  31.  
  32. /**
  33. * Removes the suggestion from the list of displayed suggestions.
  34. * If the string is not in the set then there is no change made.
  35. *
  36. * @param suggestion NSString to remove from the suggested strings
  37. */
  38. - (void)removeSuggestion:(NSString *)suggestion;
  39.  
  40. /**
  41. * Takes in either NSArray or NSSet and replaces 'suggestions' with the input.
  42. * Only the first three arguments are recognized.
  43. * Objects should be strings. Undefined behavior otherwise.
  44. *
  45. * @param suggestions NSArray or NSSet with 0-3 NSStrings
  46. */
  47. - (void)setSuggestions:(NSObject *)suggestions;
  48.  
  49. @property (weak) id <SuggestionViewDelegate> delegate;
  50.  
  51. /**
  52. * The maximum number of suggestions allowed. Default is 3.
  53. */
  54. @property (nonatomic) NSInteger maxSuggestionCount;
  55.  
  56. @end
  1. #import "SuggestionView.h"
  2.  
  3. #define kScreenWidth [UIScreen mainScreen].bounds.size.width
  4.  
  5. @implementation SuggestionView {
  6. NSMutableOrderedSet *_suggestions;
  7. NSMutableArray *_suggestionButtons;
  8. }
  9.  
  10. - (instancetype)init {
  11. self = [self initWithFrame:CGRectMake(0.0f,0.0f,kScreenWidth,36.0f)];
  12.  
  13. if (self) {
  14.  
  15. }
  16.  
  17. return self;
  18. }
  19.  
  20. - (instancetype)initWithFrame:(CGRect)frame {
  21. self = [super initWithFrame:frame inputViewStyle:UIInputViewStyleKeyboard];
  22.  
  23. if (self) {
  24. _suggestions = [[NSMutableOrderedSet alloc] initWithCapacity:3];
  25. self.maxSuggestionCount = 3;
  26. _suggestionButtons = [[NSMutableArray alloc] init];
  27. self.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.04f];
  28. }
  29.  
  30. return self;
  31. }
  32.  
  33. #pragma mark - Modifying Suggestions
  34.  
  35. - (void)addSuggestion:(NSString *)suggestion {
  36. if (suggestion) {
  37. [_suggestions addObject:suggestion];
  38. }
  39.  
  40. while (_suggestions.count > self.maxSuggestionCount) {
  41. [_suggestions removeObjectAtIndex:self.maxSuggestionCount];
  42. }
  43. }
  44.  
  45. - (void)removeSuggestion:(NSString *)suggestion {
  46. [_suggestions removeObject:suggestion];
  47. }
  48.  
  49. - (void)setSuggestions:(NSObject *)suggestions {
  50. if ([suggestions respondsToSelector:@selector(countByEnumeratingWithState:objects:count:)]) {
  51. [_suggestions removeAllObjects];
  52.  
  53. for (NSString *suggestion in (NSArray *)suggestions) {
  54. if (_suggestions.count < self.maxSuggestionCount) {
  55. [_suggestions addObject:suggestion];
  56. } else {
  57. break;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. - (NSArray *)suggestions {
  64. NSMutableArray *suggestionsArray = [[NSMutableArray alloc] initWithCapacity:_suggestions.count];
  65. for (NSString *suggestion in _suggestions) {
  66. [suggestionsArray addObject:suggestion];
  67. }
  68.  
  69. return suggestionsArray;
  70. }
  71.  
  72. #pragma mark - Visual Layout of Suggestions
  73.  
  74. - (void)layoutSubviews {
  75. [self layoutSuggestions];
  76. }
  77.  
  78. - (void)layoutSuggestions {
  79. for (UIView *subview in _suggestionButtons) {
  80. [subview removeFromSuperview];
  81. }
  82.  
  83. [_suggestionButtons removeAllObjects];
  84.  
  85. for (int i = 0; i < _suggestions.count; i++) {
  86. NSString *suggestion = _suggestions[i];
  87. UIButton *suggestionButton = [[UIButton alloc] initWithFrame:CGRectMake(i * self.bounds.size.width/_suggestions.count,self.bounds.size.width/_suggestions.count,self.bounds.size.height)];
  88. [suggestionButton setTitle:suggestion forState:UIControlStateNormal];
  89. suggestionButton.titleLabel.adjustsFontSizeToFitWidth = YES;
  90. suggestionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
  91. [suggestionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  92. [suggestionButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
  93. [self addSubview:suggestionButton];
  94.  
  95. if (i > 0) {
  96. UIView *whiteLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.5f,self.bounds.size.height)];
  97. whiteLine.backgroundColor = [UIColor whiteColor];
  98. [suggestionButton addSubview:whiteLine];
  99. }
  100.  
  101. [_suggestionButtons addObject:suggestionButton];
  102. }
  103. }
  104.  
  105. #pragma mark - Selecting a Suggestion
  106.  
  107. - (void)buttonTouched:(UIButton *)button {
  108. NSTimeInterval animationDuration = 0.09f;
  109. [UIView animateWithDuration:animationDuration animations:^{
  110. [button setBackgroundColor:[UIColor whiteColor]];
  111.  
  112. if ([self.delegate respondsToSelector:@selector(suggestionSelected:)]) {
  113. [self performSelector:@selector(suggestionSelected:) withObject:button.currentTitle afterDelay:animationDuration * 0.9f];
  114. }
  115.  
  116. [button performSelector:@selector(setBackgroundColor:) withObject:[UIColor clearColor] afterDelay:animationDuration];
  117. }];
  118. }
  119.  
  120. - (void)suggestionSelected:(NSString *)suggestion {
  121. if ([self.delegate respondsToSelector:@selector(suggestionSelected:)]) {
  122. [self.delegate suggestionSelected:suggestion];
  123. }
  124. }
  125.  
  126. @end

要将其实现为您已经子类化的UITextField或UITextView,请导入SuggestionView并实现SuggestionViewDelegate.然后,在UITextFieldDelegate(或UITextViewDelegate)方法中,添加

  1. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  2. if ([textField isEqual:self.messageTextField]) {
  3. if (self.suggestionView.suggestions.count > 0 && textField.text.length == 0) {
  4. textField.inputAccessoryView = self.suggestionView;
  5. textField.autocorrectionType = UITextAutocorrectionTypeNo;
  6. [textField reloadInputViews];
  7. }
  8. }
  9.  
  10. return YES;
  11. }
  12.  
  13. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  14. if ([textField isEqual:self.messageTextField]) {
  15. if (string.length > 0) {
  16. [self removeSuggestionView];
  17. }
  18. }
  19.  
  20. return YES;
  21. }
  22.  
  23.  
  24. - (void)removeSuggestionView {
  25. self.messageTextField.inputAccessoryView = nil;
  26. [self.messageTextField setInputAccessoryView:nil];
  27. self.messageTextField.autocorrectionType = UITextAutocorrectionTypeYes;
  28. [self.messageTextField reloadInputViews];
  29.  
  30. [self.messageTextField performSelector:@selector(resignFirstResponder) withObject:self afterDelay:0.0f];
  31. [self.messageTextField performSelector:@selector(becomeFirstResponder) withObject:self afterDelay:0.0f];
  32. }

然后,实现SuggestionViewDelegate:

  1. - (void)suggestionSelected:(NSString *)suggestion {
  2. [self.messageTextField setText:[NSString stringWithFormat:@"%@%@ ",self.messageTextField.text,suggestion]];
  3. [self removeSuggestionView];
  4. }

虽然这不是一个完美的解决方案,但它确实产生了预期的效果.

猜你在找的iOS相关文章