iOS textfield 限制只能输入中文

网上搜到的大都是同一种方法,但是这种方法在全键的情况下可以,在九键的情况下是存在问题的,弄得头疼,后来才发现,九键输入的字符是需要特殊处理的

而且光处理九键还是会有问题,干脆自己写吧

.h文件

#import <Foundation/Foundation.h>

@interface ChineseNotificationHelp : NSObject

+ (instancetype)ChineseNotificationHelpShareWithTextFiled:(UITextField *)textFiled;

@end

.m文件

#import "ChineseNotificationHelp.h"

 

@interface ChineseNotificationHelp ()

@property (nonatomic,strong)UITextField *textFiled;

@property (nonatomic,strong)NSString *beforeString;

@end

 

@implementation ChineseNotificationHelp

 

+ (instancetype)ChineseNotificationHelpShareWithTextFiled:(UITextField *)textFiled{

    static ChineseNotificationHelp *share = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        share = [[ChineseNotificationHelp alloc] init];

    });

    share.textFiled = textFiled;

    return share;

}

- (instancetype)init{

    self = [super init];

    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged)

                                                   name:@"UITextFieldTextDidChangeNotification"

                                                 object:nil];

    }

    return self;

}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];

}

-(void)textFiledEditChanged{

    [self textFiledTextChange:self.textFiled];

}

- (void)textFiledTextChange:(UITextField *)textField{

    NSString *string = textField.text;

    NSLog(@"1.=%@",textField.text);

    NSArray *currentar = [UITextInputMode activeInputModes];// 键盘输入模式

    UITextInputMode *current = [currentar firstObject];

    NSString *lang = current.primaryLanguage;

    if(![lang isEqualToString:@"zh-Hans"]) return;

    UITextRange *selectedRange = [textField markedTextRange];

    //获取高亮部分

    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

    if(position) return;// 有高亮则不处理

    // 过滤非汉字

    NSMutableString *chinese = string.mutableCopy;

    for (int i = 0; i<string.length; i++) {

        int a = [string characterAtIndex:i];

        if (a > 0x4e00 && a < 0x9fff) continue;

        [chinese replaceCharactersInRange:NSMakeRange(i,1) withString:@" "];

    }

    // 除去所有空格

    textField.text = [self removeSpaceAndNewline:chinese];

    NSLog(@"2.=%@",textField.text);

    if(self.beforeString && self.beforeString.length > textField.text.length && [chinese containsString:@" "]){

        //英文切换过程中处理出现删减的bug

        textField.text = self.beforeString;

        return;

    }

    self.beforeString = textField.text;

}

// 删除kon

- (NSString *)removeSpaceAndNewline:(NSString *)str

{

    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];

    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    return temp;

}

 

@end

 

在需要做限制的textfield中一句代码即可

[ChineseNotificationHelp ChineseNotificationHelpShareWithTextFiled:textfield];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值