iOS 为UITextField添加一个最大长度的属性

首先创建一个UITextField的Category文件

#import 
IB_DESIGNABLE
@interface UITextField (DC)

/**
 设置最大长度
 */
@property (nonatomic, assign) IBInspectable NSUInteger maxLength;

@end

#import "UITextField+DC.h"
#import 

static char kMaxLength;

@interface UItextFieldMaxLengthObserver : NSObject

@end

@implementation UItextFieldMaxLengthObserver

- (void)textChange:(UITextField *)textField {
    NSString *destText = textField.text;
    NSUInteger maxLength = textField.maxLength;
    
    // 对中文的特殊处理,shouldChangeCharactersInRangeWithReplacementString 并不响应中文输入法的选择事件
    // 如拼音输入时,拼音字母处于选中状态,此时不判断是否超长
    UITextRange *selectedRange = [textField markedTextRange];
    if (!selectedRange || !selectedRange.start) {
        if (destText.length > maxLength) {
            textField.text = [destText substringToIndex:maxLength];
        }
    }
}

@end

static UItextFieldMaxLengthObserver *observer;

@implementation UITextField (DC)

@dynamic maxLength;

+ (void)load {
    observer = [[UItextFieldMaxLengthObserver alloc] init];
}

- (void)setMaxLength:(NSUInteger)maxLength {
    objc_setAssociatedObject(self, &kMaxLength, @(maxLength), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (maxLength) {
        [self addTarget:observer
                 action:@selector(textChange:)
       forControlEvents:UIControlEventEditingChanged];
    }
}

-(NSUInteger)maxLength {
    NSNumber *number = objc_getAssociatedObject(self, &kMaxLength);
    return [number integerValue];
}

@end

你可能感兴趣的:(iOS 为UITextField添加一个最大长度的属性)