需求: 要一個只能輸入中文的textfiled
難點: 中文是由拼音拼出來的末捣,拼音是英文字母
解決: 創(chuàng)建了一個Help 來負(fù)責(zé)處理和替換字符串
代碼: 代碼如下
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// 篩選管理類
@interface ChineseNotificationHelp : NSObject
+ (instancetype)ChineseNotificationHelpShareWithTextFiled:(UITextField *)textFiled;
@end
.m
#import "ChineseNotificationHelp.h"
@interface ChineseNotificationHelp ()
@property (nonatomic,strong)UITextField *textFiled;
@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;
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];
}
// 刪除kon
- (NSString *)removeSpaceAndNewline:(NSString *)str
{
NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
return temp;
}
@end
使用
// 只需要一句話就解決了 [ChineseNotificationHelp ChineseNotificationHelpShareWithTextFiled:chineseText];
chineseText = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20, 50)];
chineseText.backgroundColor = [UIColor lightGrayColor];
chineseText.placeholder =@"只能輸入漢字";
[self.view addSubview:chineseText];
[ChineseNotificationHelp ChineseNotificationHelpShareWithTextFiled:chineseText];
附加篇幅
問題:在上文的前提下英文九宮格輸入法下俗或,連點任意一個字符導(dǎo)致出現(xiàn)刪除情況
解決:
if(self.beforeString && self.beforeString.length > textField.text.length && [chinese containsString:@" "]){
//英文切換過程中處理出現(xiàn)刪減的bug
textField.text = self.beforeString;
return;
}
更正:(.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:@" "]){
//英文切換過程中處理出現(xiàn)刪減的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