首页
网站首页
公司简介
资讯中心
推荐内容
返回顶部
自定义UITextField类设计与实现,网络框架
发布时间:2020-04-22 01:19
浏览次数:

1.唐巧的技术博客

现在讲一下自定义的UITextField,这个类包含的功能暂时长度限制与emoji表情限制。主要讲下设计思路,使得自定义的UITextField更具扩展易用性。

网络框架(以下称NJAFNetworking)是基于AFNetworking框架的简单封装,基本功能包括POST请求,GET请求,上传文件,下载文件,网络状态,缓存等。

2.招聘一个靠谱的iOS

  1. 用户不需要操心基本功能的实现
  2. 肯定要具有原生UITextField的所有属性方法。
  3. 控件独立。

在使用AFNetworking的时候我们会发现对外开发接口比较多,当项目需求涉及到缓存,以及网络判断的时候,我们都无从下手。然NJAFNetworking框架将这些过多的开发接口封装到一起,让我们使用起来更为方便。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 要执行的代码});//NSEC_PER_SEC表示的是秒数,它还提供了NSEC_PER_MSEC表示毫秒。//上面这句dispatch_after的真正含义是在2秒后把任务添加进队列中,并不是表示在2秒后执行,大部分情况该函数能达到我们的预期,只有在对时间要求非常精准的情况下才可能会出现问题。

3.SourceTree入门基础

具体设计

首先自定义类HsUserTextField,继承自UITextField,同时定义帮助类HsUserTextFieldHelper,实现UITextField的协议,毕竟自身的delegate不能是自己(==会导致死循环==)所以都让帮助类去实现方法。同时帮助类还可以直接用于其它的UITextField类。

@interface HsUserTextFieldHelper : NSObject <UITextFieldDelegate>@property (nonatomic, weak) HsUserTextField *helpObject;@property (nonatomic, weak) id<HsUserTextFieldDelegate> delegate;- textFiledEditChanged;- (NSString *)setMarkedText:(NSString *)markedText selectedRange:selectedRange;@end@interface HsUserTextField()@property (nonatomic, strong)HsUserTextFieldHelper *helper;@end@implementation HsUserTextField- (instancetype)initWithFrame:frame{ self = [super initWithFrame:frame]; if  { [self addTarget:self.helper action:@selector(textFiledEditChanged) forControlEvents:UIControlEventEditingChanged]; super.delegate = self.helper; self.supportEmoji = NO; } return self;}- (HsUserTextFieldHelper *)helper{ if (_helper == nil) { _helper = [[HsUserTextFieldHelper alloc] init]; _helper.helpObject = self; } return _helper;}- setDelegate:(id<HsUserTextFieldDelegate>)delegate{ self.helper.delegate = delegate; super.delegate = self.helper;}- setMarkedText:(NSString *)markedText selectedRange:selectedRange{ NSString *helpMarkedText = [self.helper setMarkedText:markedText selectedRange:selectedRange]; [super setMarkedText:helpMarkedText selectedRange:selectedRange];}- dealloc{ }@end@implementation HsUserTextFieldHelper- nowIsEmojiKeyBorad{ for (NSString *keyboardInputMode in [UITextInputMode activeInputModes]) { if ([[keyboardInputMode valueForKey:PrimaryLanguage] isEqualToString:EmojiprimaryLanguage]) { NSNumber *isDisplayed = [keyboardInputMode valueForKey:IsDisplayed]; if ([isDisplayed boolValue] == YES) { return YES; } } } return false;}- (NSString *)setMarkedText:(NSString *)markedText selectedRange:selectedRange{ if (self.helpObject.supportEmoji == NO) { //表情检查 if ([HsUtil checkEmoji:markedText]) { return @""; } } return markedText;}- textFiledEditChanged{ if (self.helpObject.maxLength != 0) { NSInteger kmaxLength = self.helpObject.maxLength; NSString *toBeString = self.helpObject.text; //截取长度 if (toBeString.length > kmaxLength) { self.helpObject.text = [toBeString substringToIndex:kmaxLength]; } } if ([self.delegate respondsToSelector:@selector(userTextFieldEditChanged:)]) { [self.delegate userTextFieldEditChanged:self.helpObject]; }}- textField:(UITextField *)textField shouldChangeCharactersInRange:range replacementString:(NSString *)string{ //emoji表情校验 if (self.helpObject.supportEmoji == NO && string.length != 0) { if ([self nowIsEmojiKeyBorad] == YES) { return NO; } } //长度校验 if (self.helpObject.maxLength != 0 && string.length != 0) { NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSInteger kmaxLength = self.helpObject.maxLength; if (toBeString.length > kmaxLength){ return NO; } } if ([self.delegate respondsToSelector:@selector(userTextField:shouldChangeCharactersInRange:replacementString:)]) { return [self.delegate userTextField:self.helpObject shouldChangeCharactersInRange:range replacementString:string]; } return YES;}- textFieldDidEndEditing:(UITextField *)textField{ if ([self.delegate respondsToSelector:@selector(userTextFieldDidEndEditing:)]) { [self.delegate userTextFieldDidEndEditing:self.helpObject]; }}- textFieldDidBeginEditing:(UITextField *)textField{ if ([self.delegate respondsToSelector:@selector(userTextField:textFieldDidBeginEditing:)]) { [self.delegate userTextFieldDidBeginEditing:self.helpObject]; }}@end
  • 优点:
    • www.64222.com ,网络状态已经封装,在通过GET/POST请求会回调;
    • 缓存已经封装,当GET/POST请求失败后会回调;
    • 开发接口少,只需调用相应功能功能的接口即可。

1.performSelector方法

4.CocoaPods使用教程

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];此方式要求必须在主线程中执行,否则无效。是一种非阻塞的执行方式,暂时未找到取消执行的方法。

友情链接: 网站地图
Copyright © 2015-2019 http://www.nflfreepicks.net. 新葡萄京娱乐场网址有限公司 版权所有