此项目旨在提供一个简单可自定义的文本字段,并具有分组功能。
请在我们的博文中查看这篇文章:博客文章。
LUNField 是一个类,用于简化文本字段的分组并提供了美观的动画。它包括验证内容以吸引用户注意的功能。您还可以将其用作普通文本字段,但具有动态的占位符和美观的边框动画。例如,您可以用作信用卡号码或手机输入元素。
LUNField 需要 ARC。
LUNField 从 UIView 派生,与所有 UIKit 组件一样,它应该只从主线程访问。
要在应用中使用 LUNField 类,只需将 LUNField 类文件(不需要示例文件和资产)拖放到您的项目中。
您还可以使用 CocoaPods
pod 'LUNField'
@property (weak, nonatomic) IBOutlet id <LUNFieldDataSource> dataSource;
一个支持 LUNFieldDataSource
协议的对象,并提供了构建和修改 LUNField 的可能性。
@property (weak, nonatomic) IBOutlet id <LUNFieldDelegate> delegate;
一个支持 LUNFieldDelegate
协议的对象,可以响应该 LUNField 的事件。
@property (strong, nonatomic) NSString *text;
文本字段显示的文本。
@property (assign, nonatomic) UIKeyboardType keyboardType;
与文本对象相关联的键盘样式。
@property (strong, nonatomic) UIFont *placeholderFont;
@property (strong, nonatomic) IBInspectable NSString *placeholderText;
@property (strong, nonatomic) IBInspectable UIColor *placeholderFontColor;
@property (strong, nonatomic) IBInspectable UIColor *upperPlaceholderFontColor;
@property (strong, nonatomic) IBInspectable UIImage *placeholderImage;
这些属性自定义 LUNField 的占位符。
@property (strong, nonatomic) UIFont *textFont;
@property (strong, nonatomic) IBInspectable UIColor *textFontColor;
@property (strong, nonatomic) IBInspectable UIColor *tintColor;
这些属性自定义 LUNField 中的文本。
@property (assign, nonatomic) IBInspectable CGFloat borderWidth;
@property (strong, nonatomic) IBInspectable UIColor *borderColor;
@property (strong, nonatomic) IBInspectable UIColor *upperBorderColor;
这些属性自定义 LUNField 中的边框。
@property (strong, nonatomic) IBInspectable UIColor *underliningColor;
如果 borderColor 为 nil,则此属性自定义 LUNField 的下划线。
@property (strong, nonatomic) IBInspectable NSString *correctLabelText;
@property (strong, nonatomic) IBInspectable UIColor *correctStateBorderColor;
@property (strong, nonatomic) IBInspectable UIColor *correctStatePlaceholderLabelTextColor;
@property (strong, nonatomic) IBInspectable UIImage *correctStateImage;
这些属性自定义验证成功后的 LUNField 状态。
@property (strong, nonatomic) IBInspectable NSString *incorrectLabelText;
@property (strong, nonatomic) IBInspectable UIColor *incorrectStateBorderColor;
@property (strong, nonatomic) IBInspectable UIColor *incorrectStatePlaceholderLabelTextColor;
@property (strong, nonatomic) IBInspectable UIImage *incorrectStateImage;
这些属性自定义验证失败后的 LUNField 状态。
@property (strong, nonatomic) UIView *leftView;
此属性类似于文本字段的 leftView。只有当 LUNField 中的 numberOfSections 等于 1 时,它才会影响 LUNField。
@property (strong, nonatomic) UIView *accessoryView;
此属性类似于文本字段的 inputAccessoryView。
@property (assign, nonatomic, readonly) LUNValidationOfContent isCorrect;
此属性包含验证结果。
@property (assign, nonatomic) LUNAccessoryViewMode accessoryViewMode;
此属性负责显示辅视图。定义它是否在各个部分显示,还是仅在每个部分的最后一个显示。
@property (assign, nonatomic) LUNPlaceholderAlignment placeholderAlignment;
此属性定义了在LUNField中占位符标签的对齐方式。
LUNField类具有以下方法:
- (BOOL)isFirstResponder;
返回一个布尔值,指示LUNField是否是第一个响应者。
- (void)becomeFirstResponder;
LUNField的第一个未填充的文本字段变为第一个响应者。
- (void)resignFirstResponder;
LUNField的文本字段注销第一个响应者。
- (void)reload;
此操作从dataSource重新加载LUNField并刷新显示。不影响文本。
此外,LUNField还包含几个fabric方法,有助于使用默认模板自定义LUNField。
+ (LUNField *)LUNBorderedFieldWithDataSource:(id<LUNFieldDataSource>)dataSource delegate:(id<LUNFieldDelegate>)delegate;
+ (LUNField *)LUNUnderlinedFieldWithDataSource:(id<LUNFieldDataSource>)dataSource delegate:(id<LUNFieldDelegate>)delegate;
+ (LUNField *)LUNBorderedFieldWithWithDataSource:(id<LUNFieldDataSource> )dataSource delegate:(id<LUNFieldDelegate>)delegate placeholderText:(NSString *)placeholderText;
+ (LUNField *)LUNBorderedFieldWithDataSource:(id<LUNFieldDataSource>)dataSource delegate:(id<LUNFieldDelegate>)delegate placeholderText:(NSString *)placeholderText borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor upperBorderColor:(UIColor *)upperBorderColor;
+ (LUNField *)LUNUnderlinedFieldWithDataSource:(id<LUNFieldDataSource>)dataSource delegate:(id<LUNFieldDelegate>)delegate underliningHeight:(CGFloat)underliningHeight underliningColor:(UIColor *)underliningColor;
LUNField通过提供两个协议接口遵从Apple的数据驱动视图约定,即LUNFieldDataSource
和LUNFieldDelegate
。
LUNFieldDataSource协议有以下必需方法:
- (NSUInteger)numberOfSectionsInTextField:(LUNField *)LUNField;
返回LUNField中的部分(UITextFields)数。
- (NSUInteger)numberOfCharactersInSection:(NSInteger)section inTextField:(LUNField *)LUNField;
返回LUNField中请求部分的最大符号数。LUNField的部分按此数字成比例构建。
LUNFieldDelegate协议有以下可选方法:
- (void)LUNFieldTextChanged:(LUNField *)LUNField;
通知LUNField中的文本已更改。
- (void)LUNFieldHasEndedEditing:(LUNField *)LUNField;
通知LUNField编辑结束。
- (BOOL)LUNField:(LUNField *)LUNField containsValidText:(NSString *)text;
询问LUNField中的文本是否有效;当LUNField编辑结束时调用。
使用条款受MIT许可证约束。