3

我有一个继承自UIView并符合UIKeyInput * .h *的类

@interface UIKeyInputExampleView : UIView  <UIKeyInput>{
    NSMutableString *textStore;
}

@property (nonatomic, retain) NSMutableString *textStore;

@end

.m

@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.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];
    }

    @end

当我使用英语或越南语键盘时,一切正常。但是当我使用日文键盘时,没有调用任何事件,也没有抛出异常。我想我没有遵守一些协议

你能帮助我吗?

4

2 回答 2

5

花了一些时间,但最终它起作用了。
1. 对于单个字符 - 我仍然使用 UIKeyInput 协议。
2. 对于东亚语言,我使用 NSString 属性 intlInput 来获取所有输入字符。以下是允许执行此操作的 UITextInput 协议的两种方法。

- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange {
    self.intlInput = markedText;
}
- (void) unmarkText {
    if (!self.intlInput) return;
    for (int i=0;i<self.intlInput.length;i++) {
        [self sendChar:[self.intlInput characterAtIndex:i]];
    }
    self.intlInput = nil;
}

sendChar - 是调用以获取组合输入的所有字符的任何方法。您可以在未标记文本时发布通知(选择了某种组合)。

于 2013-11-18T12:15:52.037 回答
-1

我已经解决了这个问题,这个例子展示了如何在 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
于 2018-11-29T03:04:20.727 回答