效果图:

分别写一个类继承与UITextField和UITextView,现在以UITextField为列,UITextView同理
CustomizeTextField.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CustomizeTextField : UITextField
@end
NS_ASSUME_NONNULL_END
CustomizeTextField.m
#import "CustomizeTextField.h"
@implementation CustomizeTextField
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
[self setDefaultInputAccessoryViewWithTarget:self action:@selector(numberFieldCancle)];
}
- (void)setDefaultInputAccessoryViewWithTarget:(id)target action:(SEL) action{
UIToolbar *toolBar =[UIToolbar new];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:action];
doneBtn.tintColor=[UIColor blueColor];
NSArray*items =@[flexSpace,doneBtn];
toolBar.items= items;
[toolBar sizeToFit];
self.inputAccessoryView = toolBar;
}
- (void)numberFieldCancle{
[self resignFirstResponder]; //收起键盘
}
@end
本文介绍如何通过创建自定义类CustomizeTextField继承UITextField,实现对文本字段的外观和功能进行自定义,包括设置输入视图和取消按钮。同时,提供了一个用于收起键盘的方法numberFieldCancle。
906

被折叠的 条评论
为什么被折叠?



