小例子:runtime实现UITextView占位字符,最大输入

2018-07-20  本文已影响34人  StoneWing

日常开发中,我们经常使用UITextView,然而系统自身的UITextView并不支持占位字符的设置,限制最大输入字符数的时候,又需要去实现协议,在协议中去做操作


runtime实现UITextView占位字符

 (void)setPlaceHolder:(NSString *)placeHolder {
    objc_setAssociatedObject(self, @"placeHolderKey", placeHolder, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)placeHolder {
    return objc_getAssociatedObject(self, @"placeHolderKey");
}
- (UILabel *)placeHolderLabel {
    UILabel *placeHolderLabel = objc_getAssociatedObject(self, @"placeHolderLabel");
    if (!placeHolderLabel) {
        
        placeHolderLabel = [UILabel new];
        objc_setAssociatedObject(self, @"placeHolderLabel", placeHolderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self insertSubview:placeHolderLabel atIndex:0];
        UIEdgeInsets textContainerInset = self.textContainerInset;
        CGFloat lineFragmentPadding = self.textContainer.lineFragmentPadding;
        CGFloat x = lineFragmentPadding + textContainerInset.left + self.layer.borderWidth; //uitextview的左间距
        CGFloat y = textContainerInset.top + self.layer.borderWidth; // 上间距

         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
        
        [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(x);
            make.top.mas_equalTo(y);
            make.width.mas_equalTo(self);
            make.height.mas_greaterThanOrEqualTo(0);
            
        }];
    }
}
- (void)textDidChange {
    if (self.placeHolder) {
        if (self.text.length) {
            self.placeHolderLabel.hidden = YES;
        }else {
            
            self.placeHolderLabel.hidden = NO;
            self.placeHolderLabel.font = self.font?self.font:self.cacutDefaultFont;
            self.placeHolderLabel.textAlignment = self.textAlignment;
            self.placeHolderLabel.text = self.placeHolder;
            
        }
    }else {
        self.placeHolderLabel.hidden = YES;
    }
}
+ (void)load {
    
    [super load];
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(setText:)), class_getInstanceMethod(self, @selector(wsl_setText:)));
}
- (void)wsl_setText: (NSString *)text {
    
    [self wsl_setText:text];
    [self textDidChange];
}
+ (void)load {
    
    [super load];
    
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(delegate)), class_getInstanceMethod(self, @selector(wsl_delloc)));
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(setText:)), class_getInstanceMethod(self, @selector(wsl_setText:)));
}
- (void)wsl_delloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self wsl_delloc];
}

runtime实现最大字符数限制

- (void)setMaxNum:(NSInteger)maxNum {
    objc_setAssociatedObject(self, @"maxNum", @(maxNum), OBJC_ASSOCIATION_ASSIGN);
//这里我们在set方法中创建了通知来监听text的改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}
- (NSInteger)maxNum {
    return [objc_getAssociatedObject(self, @"maxNum") integerValue];
}
NSString  *nsTextContent = self.text;
    NSInteger existTextNum = nsTextContent.length;
    
    if (self.maxNum && existTextNum > self.maxNum) {
        //截取到最大位置的字符
        NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
        if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
            UITextRange *selectedRange = [self markedTextRange];
            //获取高亮部分
            UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
            if (!position) {// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
                if (nsTextContent.length > self.maxNum) {
                    self.text = [nsTextContent substringToIndex:self.maxNum];
                }
            }
            else{//有高亮选择的字符串,则暂不对文字进行统计和限制
            }
        }else{//中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
            if (nsTextContent.length > self.maxNum) {
                self.text = [nsTextContent substringToIndex:self.maxNum];
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读