1. textField輸入金額种玛、數(shù)字、整數(shù)致燥、小數(shù) 輸入判斷
#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.輸入個數(shù)限制遇上中文輸入法高亮部分的問題 + 輸入框視圖動態(tài)高度
注意點:
- 一次性編輯直接用代理登疗、通知就可以監(jiān)聽計算文字字數(shù)。
- 當存在編輯的時候 嫌蚤,代理辐益、通知在給UITextView賦值的時候,是不被調(diào)用的脱吱,所以給了一個外部方法再賦值的時候調(diào)用智政。
- 當textView輸入高度變化的時候回調(diào)給外面修改視圖的高度。
self.inputView.textView.text = _projectDetail.desc;
[self.inputView textChanged]; // 賦值后調(diào)用
.h
@interface PCInputView : UIView <UITextViewDelegate>
@property (nonatomic, strong) UILabel * nameLabel;
@property (nonatomic, strong) UILabel * countLabel;
@property (nonatomic, strong) IQTextView * textView;
// 限制文字字數(shù)
@property (nonatomic, assign) NSInteger maxCount;
// 輸入框高度變化后整體的高度
@property (nonatomic, copy) void(^inputHightBlock) (CGFloat curHight);
// 文字改變箱蝠,計算文字-給外界賦值的時候調(diào)用
- (void)textChanged;
@end
.m
#pragma mark - bindUI
- (void)bindUI {
// textField 監(jiān)聽輸入女仰、計算文字
// [self.textField addTarget:self action:@selector(editChange:) forControlEvents:UIControlEventEditingChanged];
// textView 監(jiān)聽輸入、計算文字
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangedExt:) name:UITextViewTextDidChangeNotification object:nil];
// 監(jiān)聽輸入框的高度變化
[self.textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
#pragma mark - 監(jiān)聽輸入 計算文字
- (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];
// 判斷是否存在高亮字符抡锈,如果有,則不進行字數(shù)統(tǒng)計和字符串截斷
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 監(jiān)聽輸入框的高度變化 并回調(diào)外部 修改整體高度
- (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;
}