美文网首页自鉴iOS开发资料收集区iOS 开发每天分享优质文章
UITextField最大字符数和最大字节数的限制

UITextField最大字符数和最大字节数的限制

作者: 向晨宇 | 来源:发表于2016-11-28 22:05 被阅读122次

    欢迎查看原文:

    http://www.iosxxx.com/blog/2016-11-27-UITextField%E6%9C%80%E5%A4%A7%E5%AD%97%E7%AC%A6%E6%95%B0%E5%92%8C%E6%9C%80%E5%A4%A7%E5%AD%97%E8%8A%82%E6%95%B0%E7%9A%84%E9%99%90%E5%88%B6.html

    UITextView
    ,UITextfield
    中有很多坑,网上的方法也很多,但是用过之后暂时没有发现一个好用。这里我给大家几组测试用例可以一试,为啥不好用。
    限制10个字节,输入2个Emoj之后是8个字节(一个Emoj是4个字节),此时再输入一个中文,看看结果如何(中文的UTF8占3个字节)
    限制5个字符,一个Emoj是2个字符,其他都是一个。此时输入两个Emoj,再输入中文,然后中文联想试试。

    就目前的情况来说,看了很多资料,并没有一个通用的能限制字符数和字节数的封装。这里全面进行了总结,并进行了封装。欢迎大家下载。
    一. 字符限制
    1. 错误方法
    常见的这种方法是错误的,会导致Emoj表情的截取问题
    12345678910

    • (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;}

    这种限制方法会导致拼音下出现这种情况,且无法输入.无法输入满5个字符。在emoj表情也有问题



    打印userinfo:
    1234567891011

    (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;}

    此时我们去按123旁边的小圆球会出现如下的图:


    打印userinfo:
    1234567891011

    (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;}

    键盘被遮挡了。
    总结:观察结果,发现了这个规律,打印一下时间,还有一个问题就是,中文键盘第一次启动的时候会回调两次。
    1

    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    所以去掉这句话即可
    六. 使用封装的XXTextField
    UITextView
    ,UITextfield
    中如果有keyboard的时候,需要一个自动弹起事件,以及弹起之后的content的偏移对父view的处理。如果每个页面都实现一次会非常复杂。这里我们介绍一种自动化的处理机制。在此之前,先介绍一下文字处理框架.最后给大家推荐一下我写的XXTextField
    ,大家也可以在此基础上自己添加一些正则表达式。使用方法很简单.欢迎加入QQ群:237305299 ,一起探讨iOS技术问题
    1.解决uiview中的textfield 遮挡问题
    123456789101112131415161718192021222324252627282930313233343536373839404142434445

    _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

    2.解决uitableview中键盘遮挡问题
    123456789101112131415161718192021222324252627282930

    /* * 键盘要显示的时候 /- (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;}

    下载地址:xxtextfield

    相关文章

      网友评论

      • hu9134:建议修改一个排版,这个支持markdown的
        超_iOS:这排版也是醉了

      本文标题:UITextField最大字符数和最大字节数的限制

      本文链接:https://www.haomeiwen.com/subject/whxapttx.html