iOS輸入框限制 統計

iOS常用正則表達式
簡單完美的解決鍵盤遮擋輸入框的問題

1. textField輸入金額、數字、整數、小數 輸入判斷

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *toString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toString.length) {
        NSString *stringRegex;
        if (textField == self.teamPriceTF || textField == self.masterPriceTF) {
            // 0~99999
            stringRegex = @"(\\+)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,4}(([.]\\d{0,2})?)))?";
        }
        else if (textField == self.prepayPriceTF) {
            // 1~9.9
            stringRegex = @"([1-9]{1}(([.]\\d{0,1})?))?"
        }
        else if (textField == self.personCountTF) {
            // 2~10
            stringRegex = @"[2-9]|10?";
        }
        else if (textField == self.timersTF) {
            // 1~999
            stringRegex = @"[1-9]\\d{0,2}?";
        }
        else if (textField == self.personCountTF) {
            // 2~10
            stringRegex = @"[2-9]|10?";
        }
        else if (textField == self.timersTF) {
            // 1~999
            stringRegex = @"[1-9]\\d{0,2}?";
        }
        NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
        BOOL flag = [phoneTest evaluateWithObject:toString];
        return flag;
    }
    return YES;
}

2.輸入個數限制遇上中文輸入法高亮部分的問題 + 輸入框視圖動態高度

注意點:

  1. 一次性編輯直接用代理、通知就可以監聽計算文字字數。
  2. 當存在編輯的時候 ,代理、通知在給UITextView賦值的時候,是不被調用的,所以給了一個外部方法再賦值的時候調用。
  3. 當textView輸入高度變化的時候回調給外面修改視圖的高度。
    self.inputView.textView.text = _projectDetail.desc;
    [self.inputView textChanged]; // 賦值后調用
.h
@interface PCInputView : UIView <UITextViewDelegate>

@property (nonatomic, strong) UILabel * nameLabel;
@property (nonatomic, strong) UILabel * countLabel;
@property (nonatomic, strong) IQTextView * textView;

// 限制文字字數
@property (nonatomic, assign) NSInteger maxCount;

// 輸入框高度變化后整體的高度
@property (nonatomic, copy) void(^inputHightBlock) (CGFloat curHight);

// 文字改變,計算文字-給外界賦值的時候調用
- (void)textChanged;

@end
.m
#pragma mark - bindUI

- (void)bindUI {
    // textField 監聽輸入、計算文字
//    [self.textField addTarget:self action:@selector(editChange:) forControlEvents:UIControlEventEditingChanged];

     // textView 監聽輸入、計算文字
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangedExt:) name:UITextViewTextDidChangeNotification object:nil];
    // 監聽輸入框的高度變化
    [self.textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

}

#pragma mark - 監聽輸入 計算文字

- (void)textChangedExt:(NSNotification *)notification {
    [self textChanged];
}

- (void)textChanged {
    _maxCount = 20;
    NSString *toString = self.textView.text;
    // 獲取高亮部分
    UITextRange *selectedRange = [self.textView markedTextRange];
    UITextPosition *position = [self.textView positionFromPosition:selectedRange.start offset:0];
    
    // 判斷是否存在高亮字符,如果有,則不進行字數統計和字符串截斷
    if (!position) {
        if (toString.length > _maxCount)  {
            self.textView.text = [toString substringToIndex:_maxCount];
        }
        self.countLabel.text = [NSString stringWithFormat:@"%zd/%zd", self.textView.text.length, _maxCount];
    }
}

#pragma mark - kvo 監聽輸入框的高度變化 并回調外部 修改整體高度

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    UITextView *textView = (UITextView*)object;

    if([keyPath isEqualToString:@"contentSize"]) {
        textView.height = textView.contentSize.height;
        NSLog(@"%f", textView.height);
        !self.inputHightBlock ? : self.inputHightBlock(69+10+textView.height);
    }
}

#pragma mark - textView delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([text isEqualToString:@"\n"]){
        [self endEditing:YES];
        return NO;
    }
    
    return YES;
}

3.去掉首尾空格

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    // 將字符串首尾空格去掉
    textField.text = [textField.text  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return YES;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容