iOS UITextField 限制输入长度,中英文混排,汉字占2个长度,英文数字占1个长度
2016-04-12 11:31
816 查看
产品有这样的需求
![](http://img.blog.csdn.net/20160412112632955)
咋一看,比较简单,但涉及到中英混排就比较麻烦了,话不多说,上代码!
其中 工具类的convertToInt:方法如下
主要就是用计算string的长度 汉字2个长度,英文1个字符
咋一看,比较简单,但涉及到中英混排就比较麻烦了,话不多说,上代码!
#import <UIKit/UIKit.h> @interface UITextField (HL_LimitLength) /** * 使用时只要调用此方法,加上一个长度(int),就可以实现了字数限制,配合tools中的方法 可以支持汉字。汉字占2个长度 * * @param length */ - (void)limitTextLength:(int)length; /** * uitextField 抖动效果 */ - (void)shake; @end
// // UITextField+HL_LimitLength.m // HitLive // // Created by 王飞 on 16/4/8. // Copyright © 2016年 国广星空视频科技(北京)有限公司. All rights reserved. // #import "UITextField+HL_LimitLength.h" @implementation UITextField (HL_LimitLength) static NSString *kLimitTextLengthKey = @"kLimitTextLengthKey"; - (void)limitTextLength:(int)length { objc_setAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey), [NSNumber numberWithInt:length], OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self addTarget:self action:@selector(textFieldTextLengthLimit:) forControlEvents:UIControlEventEditingChanged]; } - (void)textFieldTextLengthLimit:(id)sender { NSNumber *lengthNumber = objc_getAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey)); int length = [lengthNumber intValue]; //下面是修改部分 bool isChinese;//判断当前输入法是否是中文 NSArray *currentar = [UITextInputMode activeInputModes]; UITextInputMode *current = [currentar firstObject]; //[[UITextInputMode currentInputMode] primaryLanguage],废弃的方法 if ([current.primaryLanguage isEqualToString: @"en-US"]) { isChinese = false; } else { isChinese = true; } if(sender == self) { // length是自己设置的位数 NSString *str = [[self text] stringByReplacingOccurrencesOfString:@"?" withString:@""]; if (isChinese) { //中文输入法下 // int chineseLength = length/2; UITextRange *selectedRange = [self markedTextRange]; //获取高亮部分 UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0]; // 没有高亮选择的字,则对已输入的文字进行字数统计和限制 if (!position) { // if ( str.length>=length) { int textcount = [Tools convertToInt:[self text]]; if ([Tools convertToInt:[self text]] >= length) { NSString *strNew = [NSString stringWithString:str]; int chineseCount = [[NSString stringWithFormat:@"%lu",textcount - str.length] intValue]; int indexCount = (length/2 -(textcount - 2*chineseCount)/2 )+ (textcount - 2*chineseCount); [self setText:[strNew substringToIndex:indexCount]]; } } else { // NSLog(@"输入的"); } }else{ if ([str length]>=length) { NSString *strNew = [NSString stringWithString:str]; [self setText:[strNew substringToIndex:length]]; } } } } - (void)shake { CAKeyframeAnimation *keyAn = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [keyAn setDuration:0.5f]; NSArray *array = [[NSArray alloc] initWithObjects: [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x-5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x+5, self.center.y)], [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)], nil]; [keyAn setValues:array]; NSArray *times = [[NSArray alloc] initWithObjects: [NSNumber numberWithFloat:0.1f], [NSNumber numberWithFloat:0.2f], [NSNumber numberWithFloat:0.3f], [NSNumber numberWithFloat:0.4f], [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:0.6f], [NSNumber numberWithFloat:0.7f], [NSNumber numberWithFloat:0.8f], [NSNumber numberWithFloat:0.9f], [NSNumber numberWithFloat:1.0f], nil]; [keyAn setKeyTimes:times]; [self.layer addAnimation:keyAn forKey:@"TextAnim"]; } @end
其中 工具类的convertToInt:方法如下
主要就是用计算string的长度 汉字2个长度,英文1个字符
+(int)convertToInt:(NSString*)strtemp { int strlength = 0; char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding]; for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) { if (*p) { p++; strlength++; } else { p++; } } return strlength; }
相关文章推荐
- 缺陷跟踪工具jira和团队协作与项目管理工具conflunce
- LeetCode 之 Increasing Triplet Subsequence
- libjpeg.so.62: no version information available (required
- 遍历jenkins build后的文件夹,找出最新“build NO.”复制到制定目录进行操作
- 第五章 consul key/value
- dequeueReusableCellWithIdentifier: 与dequeueReusableCellWithIdentifier: forIndexPath:的区别
- iPhone键盘自定义及通知
- sqlite 中的query操作
- android AsyncQueryHandler详解
- Tui-x 基础使用
- 有block回调的UIButton和Alert
- Unique Paths
- iOS _UIButton 高亮变灰 效果去掉方法
- iOS 打包上传 Version 和 Build 的意义
- 总是弹出“Building workspace has encountered a problem. Errors during build.”的窗口
- 移动UI设计-表单设计
- 移动UI设计-主要导航模式
- NGUI在5.3打包失败问题
- NGUI在5.3打包失败问题
- unable to dequeue a cell with identifier Identify - must register a nib or a class for the identifie