[iOS]UITextField和UITextView限制输入字符数量方面
程序员文章站
2022-05-31 20:46:22
...
[iOS]UITextField和UITextView限制输入字符数量方面
demo:http://download.csdn.net/download/u012881779/9960401
UITextField和UITextView限制输入字符数量,以前一直是在对应的代理方法(shouldChangeCharactersInRange:)中操作。但这样操作存在一个bug(如图),在将要超过限制时输入一串比较长的字符串,选择这段字符串时就会提示“超过限制”,实际上这时是还能继续输入几个字符的。
本文主要就是为了解决这类bug。
#import "DMViewController.h"
// 限制输入字符数
#define YH_LIMITWORD 20
#define YH_LIMITTEXTWORD 60
@interface DMViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *titleText;
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@property (strong, nonatomic) NSString *defaultText;
@end
@implementation DMViewController
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
_defaultText = @"限制输入60个字符";
// 方式一
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeAction:) name:@"UITextFieldTextDidChangeNotification" object:_titleText];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeAction:) name:@"UITextViewTextDidChangeNotification" object:_contentTextView];
// 方式二(UITextField可以在UIControlEventEditingChanged事件的响应textDidChangeAction方法)
// [_titleText addTarget:self action:@selector(textDidChangeAction:) forControlEvents:UIControlEventEditingChanged];
// 方式三(UITextView可以在代理方法textViewDidChange中调用textDidChangeAction方法)
}
#pragma mark - Notification
- (void)textDidChangeAction:(NSNotification *)sender {
UITextField *textObject;
if ([[sender class] isSubclassOfClass:[NSNotification class]]) {
textObject = sender.object;
} else {
textObject = (id)sender;
}
NSInteger limitNum;
if ([[textObject class] isSubclassOfClass:[UITextField class]]) {
limitNum = YH_LIMITWORD;
} else {
limitNum = YH_LIMITTEXTWORD;
}
NSString *toBeString = textObject.text;
// 获取高亮部分
UITextRange *selectedRange = [textObject markedTextRange];
UITextPosition *position = [textObject positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > limitNum) {
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:limitNum];
if (rangeIndex.length == 1) {
textObject.text = [toBeString substringToIndex:limitNum];
} else {
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limitNum)];
textObject.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSString *text = textView.text;
if ([text containsString:_defaultText]) {
text = [text stringByReplacingOccurrencesOfString:_defaultText withString:@""];
}
textView.text = text;
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@""]) {
textView.text = _defaultText;
}
}
- (void)textViewDidChange:(UITextView *)textView {
//[self textDidChangeAction:(id)textView];
}
@end
示意图:
上一篇: Django知识点:ORM操作