• UITextField限制输入中文字数


    前面写一了篇,UITextField Category来限制输入的字数,是有个Bug的,要输入中文时会crash。如今改动 了下。代码例如以下

    .h文件

    #import <UIKit/UIKit.h>
    
    @interface UITextField (LimitLength)
    /**
     *  使用时仅仅要调用此方法,加上一个长度(int)。就能够实现了字数限制,汉字不能够
     *
     *  @param length
     */
    - (void)limitTextLength:(int)length;
    /**
     *  uitextField 抖动效果
     */
    - (void)shake;
    @end

    .m文件

    #import "UITextField+LimitLength.h"
    #import <objc/runtime.h>
    
    @implementation UITextField (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) { //中文输入法下
                UITextRange *selectedRange = [self markedTextRange];
                //获取高亮部分
                UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
                // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
                if (!position) {
                    if ( str.length>=length) {
                        NSString *strNew = [NSString stringWithString:str];
                        [self setText:[strNew substringToIndex:length]];
                    }
                }
                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

    參考:http://www.keepwhy.com/index.php/archives/296

    http://blog.sina.com.cn/s/blog_60f977e70101g4gj.html

    http://www.2cto.com/kf/201406/312073.html


  • 相关阅读:
    P1197 [JSOI2008]星球大战[并查集+图论]
    P1955 [NOI2015]程序自动分析[离散化+并查集]
    取模运算律[简单数学]
    P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]
    P1330 封锁阳光大学[搜索+染色]
    P1168 中位数[堆 优先队列]
    P2661 信息传递[最小环+边带权并查集]
    P1080 【NOIP 2012】 国王游戏[贪心+高精度]
    P2085 最小函数值[优先队列]
    【转】priority_queue的用法
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7339615.html
Copyright © 2020-2023  润新知