现在讲一下自定义的UITextField,这个类包含的功能暂时长度限制与emoji表情限制。主要讲下设计思路,使得自定义的UITextField更具扩展易用性。
设计思路
- 用户不需要操心基本功能的实现
- 肯定要具有原生UITextField的所有属性方法。
- 控件独立。
具体设计
首先自定义类HsUserTextField,继承自UITextField,同时定义帮助类HsUserTextFieldHelper,实现UITextField的协议,毕竟自身的delegate不能是自己(==会导致死循环==)所以都让帮助类去实现方法。同时帮助类还可以直接用于其它的UITextField类。
@interface HsUserTextFieldHelper : NSObject <UITextFieldDelegate>
@property (nonatomic, weak) HsUserTextField *helpObject;
@property (nonatomic, weak) id<HsUserTextFieldDelegate> delegate;
- (void)textFiledEditChanged;
- (NSString *)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange;
@end
@interface HsUserTextField()
@property (nonatomic, strong)HsUserTextFieldHelper *helper;
@end
@implementation HsUserTextField
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[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;
}
- (void)setDelegate:(id<HsUserTextFieldDelegate>)delegate
{
self.helper.delegate = delegate;
super.delegate = self.helper;
}
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
{
NSString *helpMarkedText = [self.helper setMarkedText:markedText selectedRange:selectedRange];
[super setMarkedText:helpMarkedText selectedRange:selectedRange];
}
- (void)dealloc{
}
@end
@implementation HsUserTextFieldHelper
- (BOOL)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:(NSRange)selectedRange
{
if (self.helpObject.supportEmoji == NO) {
//表情检查
if ([HsUtil checkEmoji:markedText]) {
return @"";
}
}
return markedText;
}
- (void)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];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)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;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(userTextFieldDidEndEditing:)]) {
[self.delegate userTextFieldDidEndEditing:self.helpObject];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(userTextField:textFieldDidBeginEditing:)]) {
[self.delegate userTextFieldDidBeginEditing:self.helpObject];
}
}
@end
网友评论