我已经解决了这个问题,这个例子展示了如何在 ios 中将中文输入转换为 insertText。
#ifndef UIKeyInputExampleView_h
#define UIKeyInputExampleView_h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface UIKeyInputExampleView : UIView <UIKeyInput,UITextInput>{
NSMutableString *textStore;
}
@property (nonatomic, retain) NSString *textStore;
@end
#endif /* UIKeyInputExampleView_h */
//
// UIKeyInputExampleView.m
// CustomInput
//
// Created by Gust on 2018/11/27.
// Copyright © 2018 Gust. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "UIKeyInputExampleView.h"
@implementation UIKeyInputExampleView
@synthesize textStore;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
self.textStore = [NSMutableString string];
//[self.textStore appendString:@"Touch screen to edit."];
//[self.textStore
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)dealloc {
//[textStore dealloc];
//[super dealloc];
}
#pragma mark -
#pragma mark Respond to touch and become first responder.
- (BOOL)canBecomeFirstResponder { return YES; }
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
[self becomeFirstResponder];
}
#pragma mark -
#pragma mark Drawing
- (void)drawRect:(CGRect)rect {
CGRect rectForText = CGRectInset(rect, 20.0, 20.0);
UIRectFrame(rect);
[self.textStore drawInRect:rectForText withFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
}
#pragma mark -
#pragma mark UIKeyInput Protocol Methods
- (BOOL)hasText {
if (textStore.length > 0) {
return YES;
}
return NO;
}
- (void)insertText:(NSString *)theText {
NSLog(@"Text have just enter:%@ length=%d ascii=%d",theText,theText.length,[theText characterAtIndex:0]);
if ([theText isEqualToString:@"\n"]) {
NSLog(@"Enter have just pressed!");
[self resignFirstResponder];
}
self.textStore = (NSMutableString*)theText;
[self setNeedsDisplay];
}
- (void)deleteBackward {
self.textStore = (NSMutableString*)@"delete";
[self setNeedsDisplay];
}
@synthesize beginningOfDocument;
@synthesize endOfDocument;
@synthesize inputDelegate;
@synthesize markedTextRange;
@synthesize markedTextStyle;
@synthesize selectedTextRange;
@synthesize tokenizer;
- (UITextWritingDirection)baseWritingDirectionForPosition:(nonnull UITextPosition *)position inDirection:(UITextStorageDirection)direction {
return UITextWritingDirectionLeftToRight;
}
- (CGRect)caretRectForPosition:(nonnull UITextPosition *)position {
return CGRectMake(0, 0, 10, 30);
}
- (nullable UITextRange *)characterRangeAtPoint:(CGPoint)point {
return nil;
}
- (nullable UITextRange *)characterRangeByExtendingPosition:(nonnull UITextPosition *)position inDirection:(UITextLayoutDirection)direction {
return nil;
}
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point {
return nil;
}
- (nullable UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(nonnull UITextRange *)range {
return nil;
}
- (NSComparisonResult)comparePosition:(nonnull UITextPosition *)position toPosition:(nonnull UITextPosition *)other {
return NSOrderedSame;
}
- (CGRect)firstRectForRange:(nonnull UITextRange *)range {
return [self bounds];
}
- (NSInteger)offsetFromPosition:(nonnull UITextPosition *)from toPosition:(nonnull UITextPosition *)toPosition {
return NSIntegerMax;
}
- (nullable UITextPosition *)positionFromPosition:(nonnull UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset {
return nil;
}
- (nullable UITextPosition *)positionFromPosition:(nonnull UITextPosition *)position offset:(NSInteger)offset {
return nil;
}
- (nullable UITextPosition *)positionWithinRange:(nonnull UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction {
return nil;
}
- (void)replaceRange:(nonnull UITextRange *)range withText:(nonnull NSString *)text {
//NSLog(@"replaceRange %@",text);
}
- (nonnull NSArray<UITextSelectionRect *> *)selectionRectsForRange:(nonnull UITextRange *)range {
NSArray *arr = NULL;
arr = @[];
return arr;
}
- (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(nonnull UITextRange *)range {
//self.textStore = markedText;
}
- (void)setMarkedText:(nullable NSString *)markedText selectedRange:(NSRange)selectedRange {
//NSLog(@"setMarkedText %@",markedText);
self.textStore=markedText;
}
- (nullable NSString *)textInRange:(nonnull UITextRange *)range {
//NSLog(@"textInRange ");
return nil;
}
- (nullable UITextRange *)textRangeFromPosition:(nonnull UITextPosition *)fromPosition toPosition:(nonnull UITextPosition *)toPosition {
return nil;
}
- (void)unmarkText {
if (!self.textStore) return;
[self insertText:textStore];
self.textStore = nil;
//NSLog(@"unmarkText ");
}
- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
}
+ (nonnull instancetype)appearance {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait whenContainedIn:(nullable Class<UIAppearanceContainer>)ContainerClass, ... {
return nil;
}
+ (nonnull instancetype)appearanceForTraitCollection:(nonnull UITraitCollection *)trait whenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)containerTypes {
return nil;
}
+ (nonnull instancetype)appearanceWhenContainedIn:(nullable Class<UIAppearanceContainer>)ContainerClass, ... {
return nil;
}
+ (nonnull instancetype)appearanceWhenContainedInInstancesOfClasses:(nonnull NSArray<Class<UIAppearanceContainer>> *)containerTypes {
return nil;
}
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
}
- (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGPointMake(0, 0);
}
- (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGPointMake(0, 0);
}
- (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGRectMake(0, 0,1,1);
}
- (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(nonnull id<UICoordinateSpace>)coordinateSpace {
return CGRectMake(0, 0,1,1);
}
- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
}
- (void)setNeedsFocusUpdate {
}
- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
return YES;
}
- (void)updateFocusIfNeeded {
}
- (nonnull NSArray<id<UIFocusItem>> *)focusItemsInRect:(CGRect)rect {
NSArray *arr = NULL;
arr = @[];
return arr;
}
@end