先自定义一个UITextField的子类HLNavTextField,在控制器里调用子类的- (void)limitHansLength:(int)hans otherLength:(int)other方法来限制字数。

 

1、[UITextInputMode currentInputMode] -->不同输入法下返回的内容:

系统自带键盘:英文下返回en-US,中文下返回zh-Hans;

搜狗输入法:中英文下都返回zh-Hans;

百度输入法:中英文下都返回zh_CN;

GO输入法:中英文下都返回mul;

 

2、textField.textInputMode.primaryLanguage --> 不同输入法下返回的内容:

系统自带键盘:英文下返回en-US,中文下返回zh-Hans,emoji下返回nil;

搜狗输入法:中英文下都返回zh-Hans;

百度输入法:中英文下都返回zh_CN;

GO输入法:中英文下都返回mul;

 

#import <UIKit/UIKit.h>

@interface HLNavTextField : UITextField
- (void)limitHansLength:(int)hans otherLength:(int)other; // hans:系统中文和emoji的限制数、第三方输入法中英文限制数;other:系统自带输入法英文输入状态的限制;
@end

从我自己的项目中抽出来的,没有经过单独测试。

 1 #import "HLNavTextField.h"
 2 
 3 @interface HLNavTextField ()
 4 @property (assign, nonatomic) int hans;
 5 @property (assign, nonatomic) int other;
 6 @end
 7 
 8 @implementation HLNavTextField
 9 
10 // 用于更改Placeholder的文字大小、颜色、位置
11 - (void)drawPlaceholderInRect:(CGRect)rect {
12     NSAttributedString *placeholder = self.attributedPlaceholder;
13     NSRange range = NSMakeRange(0, self.placeholder.length);
14     NSMutableDictionary *dict = [placeholder attributesAtIndex:0 effectiveRange:&range].mutableCopy;
15     dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
16     dict[NSFontAttributeName] = [UIFont systemFontOfSize:13];
17     CGRect myRect = CGRectMake(rect.origin.x + 3, rect.origin.y + 7, rect.size.width, rect.size.height);
18     [[self placeholder] drawInRect:myRect withAttributes:dict];
19 }
20 
21 // 注意区别初始化方式
22 - (instancetype)initWithCoder:(NSCoder *)coder
23 {
24     self = [super initWithCoder:coder];
25     if (self) {
26         self.hans = -1;
27         self.other = -1;
28         // 初始化时添加监听文字的改变
29         [self addTarget:self action:@selector(searchDidChange:) forControlEvents:UIControlEventEditingChanged];
30     }
31     return self;
32 }
33 
34 - (void)limitHansLength:(int)hans otherLength:(int)other {
35     self.hans = hans; // 设置中文状态下的限制字数
36     self.other = other; // 设置状态下的限制字数
37 }
38 
39 - (void)searchDidChange:(UITextField *)textField {
40     // 判断输入状态是否为英文
41     if (![textField.textInputMode.primaryLanguage isEqualToString:@"en-US"]) {
42         // 过滤掉输入时高亮状态下的情况
43         if (!textField.markedTextRange && (self.hans >= 0) && (textField.text.length > self.hans)) {
44             [self setCaretPositionWithTextField:textField limit:self.hans];
45         }
46     } else {
47         // 英文输入状态下
48         if ((self.other >= 0) && (textField.text.length > self.other)) {
49             [self setCaretPositionWithTextField:textField limit:self.other];
50         }
51     }
52 }
53 - (void)setCaretPositionWithTextField:(UITextField *)textField limit:(NSUInteger)length {
54     // 拿到截取之前的光标位置
55     UITextPosition *selectedPosition = textField.selectedTextRange.start;
56     // 截取限制字数以内的文本
57     textField.text = [textField.text substringToIndex:length];
58     // 恢复光标的位置
59     textField.selectedTextRange = [textField textRangeFromPosition:selectedPosition toPosition:selectedPosition];
60 }
61 
62 @end

支持插入文字、选中替换文字、联想输入文字,光标定位到新输入的文字后。

 

posted on 2015-12-18 00:04  HLL1515  阅读(1019)  评论(0编辑  收藏  举报