先上图再说话#
123.png最近公司项目需求要做一个自定义价格键盘,虽说网上应该有很多demo,但是...还是自己做一个吧~也方便日后维护.
这次封装的键盘采用的是xib+代码的形式创建的,因为头部还有另外一个样式,所以第二个样式(如下图)就采用代码来创建了。
33.png
如何切换样式?
- 根据setter方法赋值的同时重新更新约束
- (void)setSaleType:(SaleType)saleType{
_saleType = saleType;
if (saleType == SaleTypeResell) {
self.selTextField = self.priceTextField;
self.topView.hidden = NO;
[self.toLabel removeFromSuperview];
[self.priceLabel1 removeFromSuperview];
[self.priceTextField1 removeFromSuperview];
[self.priceTextField2 removeFromSuperview];
}else if (saleType == SaleTypewant2By){
self.topView.hidden = YES;
self.selTextField = self.priceTextField1;
[self addSubview:self.priceLabel1];
[self addSubview:self.toLabel];
[self addSubview:self.priceTextField1];
[self addSubview:self.priceTextField2];
[self configWant2ByConstrains];
}
}
验证输入字符方法
- (BOOL)checkWith:(NSString *)str{
BOOL res = YES;
// 开头已经有0
if ([self.selTextField.text isEqualToString:@"0"]) {
if ([str isEqualToString:@"0"]) {
return NO;
}else if (![str isEqualToString:@"."]){
self.selTextField.text = @"";
}
}
// 最高100万
if ([[self.selTextField.text stringByAppendingString:str] doubleValue] > 1000000) return NO;
// 小数点后面的处理
if ([self.selTextField.text containsString:@"."]){
NSRange range = [self.selTextField.text rangeOfString:@"."];
NSString *subStr = [self.selTextField.text substringFromIndex:range.location+1];
if (subStr.length == 2) return NO;
if ([str isEqualToString:@"."]) return NO;
}
return res;
}
//紧接着点击数字的方法
- (IBAction)didClick1{
NSString *str = @"1";
if (![self checkWith:str]) return;
self.selTextField.text = [self.selTextField.text stringByAppendingString:str];
}
...
- 处理开头有0
- 需求是不能大于100万
- 小数点后面的细节处理
对外公开的方法
// 显示键盘的方法
- (void)show{
if (self.need_coverView) {
[self.superview addSubview:self.coverView];
[self.superview bringSubviewToFront:self];
[self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(self.contentHeight);
make.left.right.bottom.mas_equalTo(self.superview);
}];
}
if (self.saleType == SaleTypewant2By) [self.priceTextField1 becomeFirstResponder];
else [self.priceTextField becomeFirstResponder];
}
为键盘添加外部灰色遮罩,同时将键盘显示出来(其实此处可以加点动画).
// // 隐藏键盘
- (void)dismiss{
[self.coverView removeFromSuperview];
[self removeFromSuperview];
}
// 重置键盘
- (void)reset{
self.priceTextField.text = @"";
self.oriPriceTextField.text = @"";
self.priceTextField1.text = @"";
self.priceTextField2.text = @"";
}
然后提供对外的代理方法
- (void)salePriceKeyboardViewDidClickDismiss:(JFSalePriceKeyboardView *)priceKeyboardView;
- (void)salePriceKeyboardViewDidClickBack:(JFSalePriceKeyboardView *)priceKeyboardView;
- (void)salePriceKeyboardViewDidClickConfirm:(JFSalePriceKeyboardView *)priceKeyboardView;
<b>另外要注意的是:点击textfield
如何不弹出系统的键盘?
self.priceTextField.inputView=[[UIView alloc]initWithFrame:CGRectZero];
最后
在Controller里调用键盘:
[self.view addSubview:self.priceKeyboardView];
[self.priceKeyboardView show];
这里只是对键盘View的一个封装,具体的业务逻辑每个公司不一样,就不放出来了。<a href="https://github.com/DreamCatcherJ/SalePriceKeyboardView">代码在这里</a>
❤️别忘了点个赞哦~
网友评论