- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 5;
}
- (void)textFieldDidChange:(UITextField *)textField
{
NSString *toBeString = textField.text;
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制,防止中文被截断
if (!position){
if (toBeString.length > _maxLength){
//中文和emoj表情存在问题,需要对此进行处理
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _maxLength)];
textField.text = [toBeString substringWithRange:rangeRange];
}
}
}
- (void)textFieldDidChange:(UITextField *)textField
{
NSString *toBeString = textField.text;
//---字节处理
NSInteger bytesCount = strlen([textField.text UTF8String]);
if (bytesCount > _maxBytesLength) {
NSString *content = [textField.text subStrWithUtf8Len:(int)_maxBytesLength];
textField.text = content;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * inputString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//限制字节数
if ([inputString length] > 0){
NSInteger len = strlen([inputString UTF8String]);
if (len > _maxBytesLength){
return NO;
}
else {
return YES;
}
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
[self.view endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
[self.view addGestureRecognizer:singleTap];
}
#pragma mark- 键盘消失
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer{
[self.view endEditing:YES];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
-(BOOL) isTextFieldMatchWithRegularExpression:(NSString *)exporession{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",exporession];
return [predicate evaluateWithObject:self];
}
-(BOOL) isTextFieldIntValue{
return [self isTextFieldMatchWithRegularExpression:@"[-]{0,1}[0-9]*"];
}
-(BOOL) isTextFieldUnsignedIntValue{
return [self isTextFieldMatchWithRegularExpression:@"[0-9]+"];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGFloat offset = self.normalTextField.frame.size.height + self.normalTextField.frame.origin.y - keyboardTop;
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
if(offset > 0){
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, -offset,self.view.frame.size.width,self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
}
}
(lldb) po userInfo
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 226}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 849}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 623}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 226}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
(lldb) po userInfo
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 623}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 510}, {414, 226}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
_textfieldName.keyboardType = UIKeyboardTypeDefault;
_textfieldName.inputType = XXTextFieldTypeOnlyInt;
_textfieldName.maxLength = 5;
_textfieldPwd.inputType = XXTextFieldTypeForbidEmoj;
#import "XXKeyboardManager.h"
@interface XXCorrectVC ()<XXKeyboardManagerShowHiddenNotificationDelegate>
@end
@implementation XXCorrectVC
- (void)viewDidLoad {
[super viewDidLoad];
[[XXKeyboardManager sharedInstance] setDelegate:self];
// Do any additional setup after loading the view from its nib.
}
#pragma mark- KeyBoardShow/Hidden
- (void)showKeyboardWithRect:(CGRect)keyboardRect
withDuration:(CGFloat)animationDuration
{
CGFloat offset = self.textFieldCorrect.frame.size.height + self.textFieldCorrect.frame.origin.y - keyboardRect.origin.y;
if(offset < 0){
return;
}
[UIView animateWithDuration:animationDuration
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect rect = CGRectMake(0.0f, -offset,self.view.frame.size.width,self.view.frame.size.height);
self.view.frame = rect;
} completion:^(BOOL finished) {
}];
}
- (void)hiddenKeyboardWithRect:(CGRect)keyboardRect
withDuration:(CGFloat)animationDuration
{
[UIView animateWithDuration:animationDuration
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.textFieldCorrect.frame = self.view.bounds;
} completion:^(BOOL finished) {
}];
}
@end
/*
* 键盘要显示的时候
*/
- (void)showKeyboardWithRect:(CGRect)keyboardRect
withDuration:(CGFloat)animationDuration{
CGSize kbSize = keyboardRect.size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_baseTableView.contentInset = contentInsets;
_baseTableView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeCell.frame.origin) ) {
[_baseTableView scrollRectToVisible:_activeCell.frame animated:YES];
}
}
/*
* 键盘要消失的时候
*/
- (void)hiddenKeyboardWithRect:(CGRect)keyboardRect
withDuration:(CGFloat)animationDuration{
_baseTableView.contentInset = UIEdgeInsetsZero;
_baseTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有